Python字节码编译器;删除不必要的变量?

发布于 2024-09-18 18:47:01 字数 352 浏览 2 评论 0原文

考虑到以下情况:

def foo():
    x = a_method_returning_a_long_list()
    y = a_method_which_filters_a_list(x)
    return y

Python 的字节码编译器会保留 x & 吗? y 在内存中,或者它是否足够聪明,可以将其简化为以下内容?

def foo():
   return a_method_which_filters_a_list(a_method_returning_a_long_list())

Given the following:

def foo():
    x = a_method_returning_a_long_list()
    y = a_method_which_filters_a_list(x)
    return y

will Python's bytecode compiler keep x & y in memory, or is it clever enough to reduce it to the following?

def foo():
   return a_method_which_filters_a_list(a_method_returning_a_long_list())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-09-25 18:47:01

它将 x 和 y 保留在内存中:

import dis
dis.dis(foo)
  2           0 LOAD_GLOBAL              0 (a_method_returning_a_long_list)
              3 CALL_FUNCTION            0
              6 STORE_FAST               0 (x)

  3           9 LOAD_GLOBAL              1 (a_method_which_filters_a_list)
             12 LOAD_FAST                0 (x)
             15 CALL_FUNCTION            1
             18 STORE_FAST               1 (y)

  4          21 LOAD_FAST                1 (y)
             24 RETURN_VALUE

整个操作非常高效,因为它是使用 LOAD_FASTSTORE_FAST 代码完成的。

正如 Roadrunner-EX 在其中一条评论中所说,两个版本的 foo 使用的内存量基本相同,如 xy 只是结果的引用(即指针)。

It keeps x and y in memory:

import dis
dis.dis(foo)
  2           0 LOAD_GLOBAL              0 (a_method_returning_a_long_list)
              3 CALL_FUNCTION            0
              6 STORE_FAST               0 (x)

  3           9 LOAD_GLOBAL              1 (a_method_which_filters_a_list)
             12 LOAD_FAST                0 (x)
             15 CALL_FUNCTION            1
             18 STORE_FAST               1 (y)

  4          21 LOAD_FAST                1 (y)
             24 RETURN_VALUE

The whole operation is quite efficient, as it is done using the LOAD_FAST and STORE_FAST codes.

As Roadrunner-EX remarks in one of the comments, the amount of memory used by your two versions of foo is basically the same, as x and y are just references (i.e., pointers) to the results.

╰ゝ天使的微笑 2024-09-25 18:47:01
In [1]: import dis

In [2]: def f():
   ...:     x = f1()
   ...:     y = f2(x)
   ...:     return y
   ...: 

In [3]: dis.dis(f)
  2           0 LOAD_GLOBAL              0 (f1)
              3 CALL_FUNCTION            0
              6 STORE_FAST               0 (x)

  3           9 LOAD_GLOBAL              1 (f2)
             12 LOAD_FAST                0 (x)
             15 CALL_FUNCTION            1
             18 STORE_FAST               1 (y)

  4          21 LOAD_FAST                1 (y)
             24 RETURN_VALUE        

所以看起来这两个变量是分开保存的。

In [1]: import dis

In [2]: def f():
   ...:     x = f1()
   ...:     y = f2(x)
   ...:     return y
   ...: 

In [3]: dis.dis(f)
  2           0 LOAD_GLOBAL              0 (f1)
              3 CALL_FUNCTION            0
              6 STORE_FAST               0 (x)

  3           9 LOAD_GLOBAL              1 (f2)
             12 LOAD_FAST                0 (x)
             15 CALL_FUNCTION            1
             18 STORE_FAST               1 (y)

  4          21 LOAD_FAST                1 (y)
             24 RETURN_VALUE        

So it looks like both variables are held separately.

夏花。依旧 2024-09-25 18:47:01

我不确定,但我猜这会让它们留在记忆中,原因有两个。首先,这样做可能付出的努力超过了其价值。无论哪种方式,都不会有巨大的性能变化。其次,变量 x 和 y 本身可能占用内存(以指针/引用的形式),由于赋值的显式性质,编译器不会触及这些内存。

I'm not certain, but I would guess it would keep them in memory, for 2 reasons. First, it's probably more effort than its worth to do that. There wouldn't be a huge performance change either way. And second, the variables x and y are probably themselves taking up memory (in the form of pointers/references), which the compiler would not touch, due to the explicit nature of the assignment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文