为什么in语句比or语句快?

In my project, I had to check whether a value was either one of two values. Since I could approach this with an if or statement or an if in statement, and since I did not know which one of the two would run faster, I ran the following code to check their respective performances:

import time
import datetime
from scipy.stats import ttest_ind


def if_in(t):
    bln = False
    for z in range(400):
        if z % 100 == 0: print("test1", z)
        starttime = time.time()
        for x in range(1000000):
            for i in range(5):
                if i in [2, 4]:
                    bln = True
        t.append(time.time() - starttime)
    return t


def if_or(t):
    bln = False
    for z in range(400):
        if z % 100 == 0: print("test2", z)
        starttime = time.time()
        for x in range(1000000):
            for i in range(5):
                if i == 2 or i == 4:
                    bln = True
        t.append(time.time() - starttime)
    return t


st = time.time()

times1 = if_in([])
times2 = if_or([])

t, p = ttest_ind(times1, times2)

print("\nTotal execution time:", str(datetime.timedelta(seconds=time.time() - st)))
t1mean = sum(times1) / len(times1)
t2mean = sum(times2) / len(times2)
print("Test1 mean:", t1mean, "\nTest2 mean:", t2mean)
print("\nT-test p-score:", p)

哪个打印:

Test1 mean: 0.47915725767612455 
Test2 mean: 0.46851890563964843

T-test p-score: 0.001033983121482868

The significant p-value indicates the in statement to take significantly less time to execute than the alternative.

为什么会有这种差异?对于“或”方法,我认为当第一个条件为真时,进一步检查将停止。我再次假设,“ in”方法也是如此。但是,一个确实比另一个运行得更快。

Additionally, would this hold up for more conditions? E.g. when i should be checked to be one of 100 values?

评论