3
while(1)vs. while(True)—为什么会有区别(在python 2字节码中)?
这个问题引起了有关perl中无限循环的问题:while(1)Vs。for(;;)是否存在速度差异?,我决定在python中运行类似的比较。我期望编译器会为while(True): pass和生成相同的字节码while(1): pass,但是python2.7实际上不是这种情况。 以下脚本: 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) 产生以下结果: 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 …
115
python