条件顺序会影响绩效吗? [英] Does the Order of Conditions affect Performance?

查看:75
本文介绍了条件顺序会影响绩效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于脚本速度如何在Python中排序条件是否重要?在SQL中它不是解释器假定哪个条件排序最快的条件。

Does it matter how I order the conditions in Python in respect to the speed of the script? In SQL e.g. it does not as the "Interpreter" assumes which condition ordering would be the fastest.

在Python中,据我所知,条件的顺序将由解释器给出。因此,例如,如果我链接或条件,是否最好按假定的消耗时间对条件进行排序,因为当第一个条件不适用时,解释器甚至会停止寻找其他条件吗?

In Python, as far as I know, the order of conditions will be taken as given by the interpreter. 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将立即返回 False 甚至不尝试计算 foo ** 10000

Python will return False immediately and not even attempt to calculate foo**10000.

这篇关于条件顺序会影响绩效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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