为什么 if True 比 if 1 慢? [英] Why is if True slower than if 1?

查看:38
本文介绍了为什么 if True 比 if 1 慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在 Python 中 if Trueif 1 慢?if True 不应该比 if 1 快吗?

我正在尝试学习 timeit 模块.从基础开始,我尝试了这些:

<预><代码>>>>def test1():... 如果是真的:...返回1... 别的:...返回0>>>print timeit("test1()", setup = "from __main__ import test1")0.193144083023>>>def test2():...如果 1:...返回1... 别的:...返回0>>>print timeit("test2()", setup = "from __main__ import test2")0.162086009979>>>def test3():... 如果是真的:...返回真... 别的:...返回假>>>print timeit("test3()", setup = "from __main__ import test3")0.214574098587>>>def test4():...如果 1:...返回真... 别的:...返回假>>>print timeit("test4()", setup = "from __main__ import test4")0.160849094391

我对这些事情感到困惑:

  1. 根据 Sylvain Defresne 先生在 这个问题 中,一切都先隐式转换为 bool ,然后再检查.那么为什么 if Trueif 1 慢?
  2. 为什么 test3test1 慢,即使只有 return 值不同?
  3. 就像问题 2,但为什么 test4test2 一点点?

注意:我运行了 3 次 timeit 并取结果的平均值,然后将时间与代码一起发布在这里.

这个问题与如何进行微基准测试无关(我在这个例子中做了,但我也明白它太基础了)但是为什么检查真"变量比常量慢.

解决方案

TrueFalse 不是 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%以内,可以接受.

Why is if True slower than if 1 in Python? Shouldn't if True be faster than if 1?

I was trying to learn the timeit module. Starting with the basics, I tried these:

>>> 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

I am confused by these things:

  1. According to the response from Mr. Sylvain Defresne in this question, everything is implicitly converted to a bool first and then checked. So why is if True slower than if 1?
  2. Why is test3 slower than test1 even though only the return values are different?
  3. Like Question 2, but why is test4 a little faster than test2?

NOTE: I ran timeit three times and took the average of the results, then posted the times here along with the code.

This question does not relate to how to do micro benchmarking(which I did in this example but I also understand that it is too basic) but why checking a 'True' variable is slower than a constant.

解决方案

True and False are not keywords in Python 2.

They must resolve at runtime. This has been changed in Python 3

Same test on 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

Time error is in 1%, which is acceptable.

这篇关于为什么 if True 比 if 1 慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆