简体   繁体   English

条件顺序是否会影响绩效?

[英]Does the Order of Conditions affect Performance?

Does it matter how I order the conditions in Python in respect to the speed of the script? 相对于脚本速度,我如何在Python中排序条件是否重要? In SQL eg it does not as the "Interpreter" assumes which condition ordering would be the fastest. 在SQL中,例如,它不是“解释器”假定哪个条件排序最快。

In Python, as far as I know, the order of conditions will be taken as given by the interpreter. 据我所知,在Python中,条件的顺序将按照解释器给出的顺序进行。 So as an example, if I chain or-conditions, is it better to order the conditions by assumed time they will consume because maybe the interpreter stops even looking for the other conditions when the first one doesn't apply anyway? 因此,例如,如果我链接或条件,是否最好按假定的消耗时间对条件进行排序,因为当第一个条件不适用时,解释器甚至会停止寻找其他条件吗?

Yes, the order of conditions matters. 是的,条件的顺序很重要。 They are evaluated left-to-right unless you change that by using parentheses, for example. 除非您使用括号来更改它们,否则它们从左到右求值。

And yes, conditions are only evaluated if the outcome of the expression isn't already clear. 是的,仅当表达式的结果尚不清楚时才评估条件。 For example, in 例如,在

if 1==0 and foo**10000 > 2:

Python will return False immediately and not even attempt to calculate foo**10000 . Python将立即返回False ,甚至不会尝试计算foo**10000

Python will not reorder your conditions like in SQL, but it will short circuit . Python不会像SQL中那样重新排列您的条件,但会短路 What this means is that it will stop evaluating as soon as possible. 这意味着它将尽快停止评估。 So if you have if True == True or long_function_call(): , long_function_call() will never be evaluated. 因此,如果您拥有if True == True or long_function_call(): long_function_call()则将永远不会对long_function_call()进行评估。 This works similarly for and with something like if True == False and long_function_call(): . 这同样适用于and喜欢的东西if True == False and long_function_call(): It would be in your best interest to consider this when writing conditional statements and can result in changes in performance. 编写条件语句时考虑这一点将是您的最大利益,并且可能导致性能变化。

Boolean operators in Python are short-circuiting - as soon as the result of an expression is clear, evaluation stops. Python中的布尔运算符会短路 -一旦表达式的结果明确,评估就会停止。 This plays an important role in Python's late-binding. 这在Python的后期绑定中起着重要作用。

For example, this is a common check: 例如,这是一个常见的检查:

def do(condition_check=None):
    if condition_check is not None and condition_check():
        # do stuff

Python is in general very conservative about premature optimizations. 通常,Python对于过早的优化非常保守。 If there is any chance that something might break, Python will not try it. 如果有可能发生故障,Python将不会尝试。

If you want to check Interpreter optimizations, try the dis module. 如果要检查解释器优化,请尝试使用dis模块。 It shows the instructions actually being run by the Python core. 它显示了实际上由Python内核运行的指令。 For example, Python will resolve constant expressions ( 10**10 => 10000000000 ) and back out of an and early ( JUMP_IF_FALSE_OR_POP ). 例如,Python将解析常量表达式( 10**10 => 10000000000 ),然后退出an and早期( JUMP_IF_FALSE_OR_POP )。

dis.dis('1==0 and 10**10 > 2')
  1           0 LOAD_CONST               0 (1)
              3 LOAD_CONST               1 (0)
              6 COMPARE_OP               2 (==)
              9 JUMP_IF_FALSE_OR_POP    21
             12 LOAD_CONST               4 (10000000000)
             15 LOAD_CONST               3 (2)
             18 COMPARE_OP               4 (>)
        >>   21 RETURN_VALUE

Note that not even pypy does any further optimization on this code! 请注意,甚至pypy都没有对此代码做任何进一步的优化!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM