简体   繁体   English

两个 if 语句之间的性能差异?

[英]Performance difference between two if statements?

I asked myself if it makes a performance difference if I use an if statement with x conditions, or x if statements with only 1 condition each.我问自己,如果我使用带有 x 个条件的 if 语句或每个只有 1 个条件的 x if 语句,是否会对性能产生影响。

so for example:所以例如:
if statement with 3 conditions: if 语句有 3 个条件:

if a == 0 and b == 0 and c == 0:
    #do something

3 if statements with just one condition each: 3 个 if 语句,每个语句只有一个条件:

if a == 0:
    if b == 0:
        if c == 0:
            #do something

Why do I want to know this?为什么我想知道这个?
I have an if statement with around 30 conditions and my code gets really messi, so I thought about splitting my if-statement in half.我有一个包含大约 30 个条件的 if 语句,我的代码变得非常混乱,所以我考虑将 if 语句分成两半。 I think that the results of this examples will be the same, but I don't know if there would be a noticeable performance difference if a == 1 .我认为这个例子的结果是一样的,但我不知道如果a == 1是否会有明显的性能差异。 Would the program check all 3 conditions in the first example even if the first one (a is 1 not 0) is false?即使第一个条件(a 为 1 而不是 0)为假,程序是否会检查第一个示例中的所有 3 个条件?

With the given example, there won't be any difference in performance, even with if a == 0 and b == 0 and c == 0: it won't check b == 0 when the initial condition a == 0 itself is False.对于给定的示例,即使if a == 0 and b == 0 and c == 0: ,性能也不会有任何差异if a == 0 and b == 0 and c == 0:当初始条件b == 0时,它不会检查b == 0 a == 0本身是假的。 But considering the minimal lines of code & readability, if a == 0 and b == 0 and c == 0: would be the better option.但是考虑到最少的代码行数和可读性, if a == 0 and b == 0 and c == 0:将是更好的选择。

You can test the performance of your script using the timeit library.您可以使用 timeit 库测试脚本的性能。 I will add an example below.我将在下面添加一个示例。

import timeit

stmt1 = 'if a == 0 and b == 0 and c == 0: pass'
stmt2 = """\
if a == 0: 
    if b == 0: 
        if c == 0: 
            pass
"""
setup1 = 'a, b, c = 0, 0, 0'
setup2 = 'a, b, c = 1, 0, 0'

print(f"First statement First setup execution time = {timeit.timeit(stmt=stmt1, setup=setup1, number=10**9)}")
print(f"First statement Second setup execution time = {timeit.timeit(stmt=stmt1, setup=setup2, number=10**9)}")
print(f"Second statement First setup execution time = {timeit.timeit(stmt=stmt2, setup=setup1, number=10**9)}")
print(f"Second statement Second setup execution time = {timeit.timeit(stmt=stmt2, setup=setup2, number=10**9)}")

Output:输出:

First statement First setup execution time = 38.7665075
First statement Second setup execution time = 15.4141367
Second statement First setup execution time = 38.29726529999999
Second statement Second setup execution time = 15.527892699999995

This shows that there is negligible difference to how you format your if statement.这表明您如何格式化 if 语句的差异可以忽略不计。 But if the first condition is false then the rest of the conditions will not be checked and the execution of the code will run faster.但如果第一个条件为假,则不会检查其余条件,并且代码的执行速度会更快。

Edit:编辑:

Also after seeing wjandrea comment below I would like to add it to the answer if anyone in the future is wondering why this is.同样在看到下面的 wjandrea 评论后,如果将来有人想知道为什么会这样,我想将其添加到答案中。 Per the python wiki you can read about short-circuiting operator behavior .根据 python wiki,您可以阅读有关短路操作符行为的信息

Any time difference between your two pieces of code is complete and total noise.两段代码之间的任何时间差都是完整的,并且是总噪声。 Run your code a billion times, and the amount of time you might save between running one or the other is less than the time it took me to write these two sentences.运行你的代码十亿次,你在运行一个或另一个之间可能节省的时间少于我写这两句话所花的时间。

Seriously, if you need to be worried about the difference in time between the two pieces of code, Python is the wrong language.说真的,如果您需要担心两段代码之间的时间差异,Python 是错误的语言。

Yes!是的! I think there is an increase of productivity/performance if you can write your code in one line, since often times it can be easier to read and interpret;我认为,如果您可以在一行中编写代码,则生产力/性能会有所提高,因为通常它更易于阅读和解释; and even more so if you use descriptive variables.如果您使用描述性变量,则更是如此。

In your code, I see some things that can be improved: There is what we call the (1) assignment operator, ie the equal(=) sign;在您的代码中,我看到了一些可以改进的地方: 我们称之为 (1) 赋值运算符,即等号 (=); and (2) The comparison operator, ie the "is equal to" (==) sign, "is greater than" (>), "is less than" (<), and etc. (2)比较运算符,即“等于”(==)、“大于”(>)、“小于”(<)等。

When we define a variable, we use the assignment operator (=) sign.当我们定义一个变量时,我们使用赋值运算符(=)符号。 eg:例如:

a = 0
b = 0
c = 0

On the other hand, we use the comparison operator (such as the (==) symbol) when we make conditional statements.另一方面,我们在进行条件语句时使用比较运算符(例如 (==) 符号)。 eg:例如:

if a == 0 and b == 0 and c == 0:
    # do something

On to your second question: Would the program check all 3 conditions in the first example even if the first one (a is 1 not 0) is false?关于您的第二个问题:即使第一个条件(a 是 1 而不是 0)为假,程序是否会检查第一个示例中的所有 3 个条件?

a = 1
b = 0
c = 0

if a == 0:
    if b == 0:
        if c == 0:
            # do something

Python will compare the conditions to your assigned variables, and if the condition turns out to be False, then there will be no ERROR, but it will also not execute the instruction that you've provided in your if-statement (meaning: it will not # do something). Python 会将条件与您分配的变量进行比较,如果条件结果为 False,则不会出现 ERROR,但它也不会执行您在 if 语句中提供的指令(意思是:它将不是#做某事)。

I hope I had help you even a little bit.我希望我对你有所帮助。

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

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