Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 两个if语句之间的性能差异?_Python_If Statement_Logical Operators - Fatal编程技术网

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

Python 两个if语句之间的性能差异?,python,if-statement,logical-operators,Python,If Statement,Logical Operators,我问自己,如果使用带有x个条件的if语句,或者使用每个只有1个条件的x if语句,是否会对性能产生影响 例如: 带有3个条件的if语句: if a == 0 and b == 0 and c == 0: #do something 3个if语句,每个语句只有一个条件: if a == 0: if b == 0: if c == 0: #do something 我为什么想知道这个? 我有一个大约有30个条件的if语句,我的代码非常复杂,

我问自己,如果使用带有x个条件的if语句,或者使用每个只有1个条件的x if语句,是否会对性能产生影响

例如:
带有3个条件的if语句:

if a == 0 and b == 0 and c == 0:
    #do something
3个if语句,每个语句只有一个条件:

if a == 0:
    if b == 0:
        if c == 0:
            #do something
我为什么想知道这个?
我有一个大约有30个条件的if语句,我的代码非常复杂,所以我考虑将if语句一分为二。
我认为这个例子的结果是相同的,但我不知道如果
a==1
,是否会有明显的性能差异。即使第一个条件(a是1而不是0)为假,程序也会检查第一个例子中的所有3个条件吗?

在给定的例子中,即使使用
如果a==0和b==0和c==0:
当初始条件
a==0本身为假时,它也不会检查
b==0
。但是考虑到最小的代码行和可读性,
如果a==0、b==0和c==0:
将是更好的选择。

对于给定的示例,性能不会有任何差异,即使使用
如果a==0和b==0和c==0:
当初始条件
a==0
本身为False时,它也不会检查
b==0
。但是考虑到最小的代码行数和可读性,
如果a==0,b==0,c==0:
将是更好的选择。

两段代码之间的任何时间差都是完整的,并且完全是噪音。运行代码10亿次,运行其中一个代码所节省的时间比我写这两句话所花的时间要少

a = 1
b = 0
c = 0

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

说真的,如果您需要担心这两段代码之间的时间差,那么Python是错误的语言。

这两段代码之间的任何时间差都是完全的和完全的噪音。运行代码10亿次,运行其中一个代码所节省的时间比我写这两句话所花的时间要少


说真的,如果您需要担心两段代码之间的时间差,Python是错误的语言。

您可以使用timeit库测试脚本的性能。我将在下面添加一个示例

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)}")
输出:

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
这表明if语句的格式差别可以忽略不计。但是,如果第一个条件为false,则不会检查其余条件,代码的执行速度将更快

编辑:

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

同样,在看到下面的wjandrea评论后,如果将来有人想知道这是为什么,我想把它添加到答案中。根据python wiki,您可以阅读。

您可以使用timeit库测试脚本的性能。我将在下面添加一个示例

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)}")
输出:

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
这表明if语句的格式差别可以忽略不计。但是,如果第一个条件为false,则不会检查其余条件,代码的执行速度将更快

编辑:

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

同样,在看到下面的wjandrea评论后,如果将来有人想知道这是为什么,我想把它添加到答案中。通过python wiki,您可以阅读。

是的!我认为,如果你能在一行代码中编写代码,生产率/性能就会提高,因为通常情况下,代码更容易阅读和解释;如果你使用描述性变量,情况会更糟


在你的代码中,我看到了一些可以改进的地方:我们称之为(1)赋值运算符,即等号(=);(2)比较运算符,即“等于”(==)符号,“大于”(>),“小于”(是的!我认为如果您可以在一行中编写代码,生产率/性能会提高,因为通常情况下,它更易于阅读和解释;如果您使用描述性变量,则更是如此


在您的代码中,我看到了一些可以改进的地方:有一个我们称之为(1)赋值运算符,即等号(=);和(2)比较运算符,即“等于”(==)号,“大于”(>),“小于”(在第一个示例中,即使第一个条件(a不是0),程序是否会检查所有3个条件是假的?-不。@Sayan这样就不会使houge在性能上有所不同了?调查。即使第一个条件(a是1不是0)是假的,程序会检查第一个例子中的所有3个条件吗?-不。@Sayan这样就不会使houge在性能上有所不同了?调查。“你的两段代码之间的任何时差都是完整的和完全的噪声。”为什么?为了OP的缘故,请解释一下。你可能想提到操作员短路。“你的两段代码之间的任何时差都是完整的和完全的噪声。”。"为什么会这样?为了OP的缘故,请解释一下。你可能想提到运算符短路。很好!为了让它更好,把赋值移到setup语句中。另外,为了可读性,我更喜欢写大的数字作为指数。例如,
flat='if a==0和b==0和c==0:pass';a_0='a,b,c=0,0,0';…timeit.timeit(stmt=flat,setup=a_0,number=10**9)
@wjandrea感谢您提供的信息。我在第二条语句中留下了多行if,以表明它除了使代码更混乱之外没有任何区别。我还看到执行时间减少了,我猜是因为在使用安装程序之前,我定义了每次运行的变量,但现在它们只定义了一次?是的,没错,现在它们只定义了一次。你确定这些时间是对的吗?它们和以前一样是x8。@wjandrea我将每109次执行的次数从1亿次更改为10亿次,而不是使用108次来保持不变。没花那么长时间,我觉得数据集越大越好