而真正的1 vs [英] while 1 vs while True

查看:58
本文介绍了而真正的1 vs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[ http://www.python.org/moin/PythonSpeed]

从Py2.3开始,解释器优化''1''到单跳

单跳。相反,同时为真需要几个步骤。虽然

为了清晰起见,后者是首选,但时间要求严格的代码应使用

第一种形式。


Out of纯粹的好奇心,

为什么不是'真的''优化'?

解决方案

Timothy Fitz写道:

[ http://www.python.org/moin / PythonSpeed ]
从Py2.3开始,解释器优化''1''只是单跳。相反,同时为真需要几个步骤。虽然为了清晰起见,后者是首选,但时间关键代码应该使用
第一种形式。

出于纯粹的好奇心,
为什么不是''True True''也进行了优化?




可能与True有关和假不是

常数。

True = 0
而True:



.... print''不是无限循环。事实上,它永远不会执行。''


Dan Bishop写道:

出于纯粹的好奇心,
为什么不真实也进行了优化?



可能与真实有关和假不是
常数。




是的。即使是无也只是在2.4中成为一个常数。


我不知道真和假是否符合类似的待遇(这样做有明显的向后兼容性问题)。


干杯,

尼克。


-

Nick Coghlan | nc******@email.com |澳大利亚布里斯班

--------------------------------------- ------------------------
http://boredomandlaziness.skystorm.net


> Dan Bishop写道:

出于纯粹的好奇心,
为什么不是'真的''优化'也?

可能与真实有关。和假不是
常数。




[Nick Coghlan]是的。甚至无也只是在2.4中变成了一个常数。

我不知道''真''和'假'是否符合类似的待遇(有$ b) $ b是明显的向后兼容性问题。)




它与Py3.0之前不同。使它们成为常数会打破大量的b / b
兼容性代码:真,假=(1 == 1),(1!= 1)。

Raymond Hettinger


[ http://www.python.org/moin/PythonSpeed ]
"Starting with Py2.3, the interpreter optimizes ''while 1'' to just a
single jump. In contrast "while True" takes several more steps. While
the latter is preferred for clarity, time-critical code should use the
first form."

Out of pure curiousity,
Why wasn''t ''While True'' optimized also?

解决方案

Timothy Fitz wrote:

[ http://www.python.org/moin/PythonSpeed ]
"Starting with Py2.3, the interpreter optimizes ''while 1'' to just a
single jump. In contrast "while True" takes several more steps. While
the latter is preferred for clarity, time-critical code should use the first form."

Out of pure curiousity,
Why wasn''t ''While True'' optimized also?



Probably has something to do with "True" and "False" not being
constants.

True = 0
while True:


.... print ''Not an infinite loop. In fact, it never executes at all.''


Dan Bishop wrote:

Out of pure curiousity,
Why wasn''t ''While True'' optimized also?


Probably has something to do with "True" and "False" not being
constants.



Yup. Even ''None'' only just became a constant in 2.4.

I don''t know if ''True'' and ''False'' are in line for similar treatment (there are
obvious backwards compatibility issues in doing so).

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net


> Dan Bishop wrote:

Out of pure curiousity,
Why wasn''t ''While True'' optimized also?

Probably has something to do with "True" and "False" not being
constants.



[Nick Coghlan] Yup. Even ''None'' only just became a constant in 2.4.

I don''t know if ''True'' and ''False'' are in line for similar treatment (there are obvious backwards compatibility issues in doing so).



It is unlike to before Py3.0. Making them constants would break the reams of
compatability code: True, False = (1==1), (1!=1).
Raymond Hettinger


这篇关于而真正的1 vs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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