为什么如果True比1更慢呢?
为什么if True比Python中的if 1更慢呢? if True比if 1更快呢?
我正在尝试学习timeit模块。 从基础开始,我尝试了这些:
>>> def test1():
... if True:
... return 1
... else:
... return 0
>>> print timeit("test1()", setup = "from __main__ import test1")
0.193144083023
>>> def test2():
... if 1:
... return 1
... else:
... return 0
>>> print timeit("test2()", setup = "from __main__ import test2")
0.162086009979
>>> def test3():
... if True:
... return True
... else:
... return False
>>> print timeit("test3()", setup = "from __main__ import test3")
0.214574098587
>>> def test4():
... if 1:
... return True
... else:
... return False
>>> print timeit("test4()", setup = "from __main__ import test4")
0.160849094391
我很困惑这些事情:
bool ,然后再进行检查。 那么为什么if True比if 1更慢呢? test3比test1慢,即使只有return值不同? test4比test2快一点? 注意:我跑了三次timeit并取得了结果的平均值,然后将这些时间与代码一起公布。
这个问题并没有涉及如何做微基准测试(我在这个例子中做过,但我也明白它太基本了),但是为什么检查'True'变量比常量慢。
True和False在Python 2中不是关键字。
他们必须在运行时解决。 这已经在Python 3中进行了更改
Python 3的相同测试:
>>> timeit.timeit('test1()',setup="from __main__ import test1", number=10000000)
2.806439919999889
>>> timeit.timeit('test2()',setup="from __main__ import test2", number=10000000)
2.801301520000038
>>> timeit.timeit('test3()',setup="from __main__ import test3", number=10000000)
2.7952816800000164
>>> timeit.timeit('test4()',setup="from __main__ import test4", number=10000000)
2.7862537199999906
时间误差在1%,这是可以接受的。
字节码拆卸使差异明显。
>>> dis.dis(test1)
2 0 LOAD_GLOBAL 0 (True)
3 JUMP_IF_FALSE 5 (to 11)
6 POP_TOP
3 7 LOAD_CONST 1 (1)
10 RETURN_VALUE
>> 11 POP_TOP
5 12 LOAD_CONST 2 (0)
15 RETURN_VALUE
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
正如Kabie提到的, True和False是Python 2中的全局变量。很多东西正在进行访问。
>>> dis.dis(test2)
3 0 LOAD_CONST 1 (1)
3 RETURN_VALUE
Python编译器能够将1识别为一个持续的“真理”表达式,并优化冗余条件!
>>> dis.dis(test3)
2 0 LOAD_GLOBAL 0 (True)
3 JUMP_IF_FALSE 5 (to 11)
6 POP_TOP
3 7 LOAD_GLOBAL 0 (True)
10 RETURN_VALUE
>> 11 POP_TOP
5 12 LOAD_GLOBAL 1 (False)
15 RETURN_VALUE
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
与test1几乎相同,只有一个LOAD_GLOBAL 。
>>> dis.dis(test4)
3 0 LOAD_GLOBAL 0 (True)
3 RETURN_VALUE
见test2 。 但LOAD_GLOBAL比LOAD_CONST要LOAD_CONST 。
