Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python编译器有可能优化掉一些整数算法吗?_Python_Compiler Construction - Fatal编程技术网

Python编译器有可能优化掉一些整数算法吗?

Python编译器有可能优化掉一些整数算法吗?,python,compiler-construction,Python,Compiler Construction,受Python缓存小整数问题的启发 Python编译器是否可以在编译时将(0-6)替换为-6?下面的代码表明它不是。如果不可能,为什么不呢?我不认为0、-或6在运行时的含义会有所不同 如果这是可能的,为什么CPython不这么做 # test_integers.py def test_integers(): print "-6 is -6 ?", -6 is -6 # True print "(0 - 6) is -6 ?", (0 - 6) is -6 # False # i

受Python缓存小整数问题的启发

Python编译器是否可以在编译时将(0-6)替换为-6?下面的代码表明它不是。如果不可能,为什么不呢?我不认为
0
-
6
在运行时的含义会有所不同

如果这是可能的,为什么CPython不这么做

# test_integers.py
def test_integers():
    print "-6 is -6 ?", -6 is -6 # True
    print "(0 - 6) is -6 ?", (0 - 6) is -6 # False

# import_test_integers.py
import test_integers
test_integers.test_integers()
在这种情况下,我的Python详细信息非常依赖于实现:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2

首先,您不应该使用
is
来比较整数值以检测优化。正如你在链接的问题中所解释的那样,这与任何事情都无关。如果您想知道对您的函数执行了哪些优化,请使用模块,该模块将生成(在2.7.2中,修复
-1
打字错误后):

你看减法实际上是优化的。您还可以看到其他一些:

>>> def f():
...     x = 1+2
...     x = 2-3
...     x = 3*4
...     x = 4/5
...     x = 5**6
... 
>>> dis.dis(f)
  2           0 LOAD_CONST               7 (3)
              3 STORE_FAST               0 (x)

  3           6 LOAD_CONST               8 (-1)
              9 STORE_FAST               0 (x)

  4          12 LOAD_CONST               9 (12)
             15 STORE_FAST               0 (x)

  5          18 LOAD_CONST               4 (4)
             21 LOAD_CONST               5 (5)
             24 BINARY_DIVIDE       
             25 STORE_FAST               0 (x)

  6          28 LOAD_CONST              10 (15625)
             31 STORE_FAST               0 (x)
             34 LOAD_CONST               0 (None)
             37 RETURN_VALUE        

FWIW:这种类型的优化称为“恒定折叠”。(我不知道是不是CPython做的。)Arg。我正准备发布这样的内容。回答得好+1更正了输入错误,谢谢。仍然在消化你其余的答案这完美地回答了我的问题,但我现在更困惑于
加载常数和
共常数是如何工作的。如果我找不到或想不出答案,我会发布一个后续问题。是的,我希望我会接受它-只是推迟,以防有人发布更好的答案。
>>> def f():
...     x = 1+2
...     x = 2-3
...     x = 3*4
...     x = 4/5
...     x = 5**6
... 
>>> dis.dis(f)
  2           0 LOAD_CONST               7 (3)
              3 STORE_FAST               0 (x)

  3           6 LOAD_CONST               8 (-1)
              9 STORE_FAST               0 (x)

  4          12 LOAD_CONST               9 (12)
             15 STORE_FAST               0 (x)

  5          18 LOAD_CONST               4 (4)
             21 LOAD_CONST               5 (5)
             24 BINARY_DIVIDE       
             25 STORE_FAST               0 (x)

  6          28 LOAD_CONST              10 (15625)
             31 STORE_FAST               0 (x)
             34 LOAD_CONST               0 (None)
             37 RETURN_VALUE