while (1) vs. while(True) -- 为什么有区别(在python 2字节码中)? [英] while (1) vs. while(True) -- Why is there a difference (in python 2 bytecode)?
问题描述
对这个关于 perl 中无限循环的问题很感兴趣:while(1) 对比for (;;) 有速度差异吗?,我决定在python中运行一个类似的比较.我预计编译器会为 while(True): pass
和 while(1): pass
生成相同的字节码,但实际上在 python2 中并非如此.7.
以下脚本:
导入文件def while_one():而 1:经过def while_true():为真:经过打印(同时 1")打印(" - - - - - - - - - - - - - - ")dis.dis(while_one)打印(当真")打印(" - - - - - - - - - - - - - - ")dis.dis(while_true)
产生以下结果:
while 1-----------------------------4 0 SETUP_LOOP 3(到 6)5 >>3 JUMP_ABSOLUTE 3>>6 LOAD_CONST 0(无)9 RETURN_VALUE当真-----------------------------8 0 SETUP_LOOP 12(到 15)>>3 LOAD_GLOBAL 0(真)6 JUMP_IF_FALSE 4(到 13)9 POP_TOP9 10 JUMP_ABSOLUTE 3>>13 POP_TOP14 POP_BLOCK>>15 LOAD_CONST 0 (无)18 RETURN_VALUE
使用 while True
显然更加复杂.这是为什么?
在其他上下文中,python 的行为就像 True
等于 1:
为什么while
要区分两者?
我注意到 python3 确实使用相同的操作评估语句:
while 1-----------------------------4 0 SETUP_LOOP 3(到 6)5 >>3 JUMP_ABSOLUTE 3>>6 LOAD_CONST 0(无)9 RETURN_VALUE当真-----------------------------8 0 SETUP_LOOP 3(到 6)9 >>3 JUMP_ABSOLUTE 3>>6 LOAD_CONST 0(无)9 RETURN_VALUE
python3 中布尔值的计算方式是否有变化?
在 Python 2.x 中,True
不是关键字,而只是一个 内置全局常量,在 bool
类型中定义为 1.因此解释器仍然需要加载 True
的内容.换句话说,True
是可重新赋值的:
Python 2.7 (r27:82508, Jul 3 2010, 21:12:11)[GCC 4.0.1 (Apple Inc. build 5493)] 在达尔文上输入帮助"、版权"、信用"或许可"以获取更多信息.>>>真 = 4>>>真的4
<小时>
在 Python 3.x 它真正成为关键字和实常数:
Python 3.1.2(r312:79147,2010 年 7 月 19 日,21:03:37)[GCC 4.2.1 (Apple Inc. build 5664)] 在达尔文上输入帮助"、版权"、信用"或许可"以获取更多信息.>>>真 = 4文件<stdin>",第 1 行语法错误:分配给关键字
因此解释器可以用无限循环替换 while True:
循环.
Intrigued by this question about infinite loops in perl: while (1) Vs. for (;;) Is there a speed difference?, I decided to run a similar comparison in python. I expected that the compiler would generate the same byte code for while(True): pass
and while(1): pass
, but this is actually not the case in python2.7.
The following script:
import dis
def while_one():
while 1:
pass
def while_true():
while True:
pass
print("while 1")
print("----------------------------")
dis.dis(while_one)
print("while True")
print("----------------------------")
dis.dis(while_true)
produces the following results:
while 1
----------------------------
4 0 SETUP_LOOP 3 (to 6)
5 >> 3 JUMP_ABSOLUTE 3
>> 6 LOAD_CONST 0 (None)
9 RETURN_VALUE
while True
----------------------------
8 0 SETUP_LOOP 12 (to 15)
>> 3 LOAD_GLOBAL 0 (True)
6 JUMP_IF_FALSE 4 (to 13)
9 POP_TOP
9 10 JUMP_ABSOLUTE 3
>> 13 POP_TOP
14 POP_BLOCK
>> 15 LOAD_CONST 0 (None)
18 RETURN_VALUE
Using while True
is noticeably more complicated. Why is this?
In other contexts, python acts as though True
equals 1:
>>> True == 1
True
>>> True + True
2
Why does while
distinguish the two?
I noticed that python3 does evaluate the statements using identical operations:
while 1
----------------------------
4 0 SETUP_LOOP 3 (to 6)
5 >> 3 JUMP_ABSOLUTE 3
>> 6 LOAD_CONST 0 (None)
9 RETURN_VALUE
while True
----------------------------
8 0 SETUP_LOOP 3 (to 6)
9 >> 3 JUMP_ABSOLUTE 3
>> 6 LOAD_CONST 0 (None)
9 RETURN_VALUE
Is there a change in python3 to the way booleans are evaluated?
In Python 2.x, True
is not a keyword, but just a built-in global constant that is defined to 1 in the bool
type. Therefore the interpreter still has to load the contents of True
. In other words, True
is reassignable:
Python 2.7 (r27:82508, Jul 3 2010, 21:12:11)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> True = 4
>>> True
4
In Python 3.x it truly becomes a keyword and a real constant:
Python 3.1.2 (r312:79147, Jul 19 2010, 21:03:37)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> True = 4
File "<stdin>", line 1
SyntaxError: assignment to keyword
thus the interpreter can replace the while True:
loop with an infinite loop.
这篇关于while (1) vs. while(True) -- 为什么有区别(在python 2字节码中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!