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)
Which printed:
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.
Why is this difference? For the 'or' method, I would assume further checking would stop when the first condition is deemed to be true. The same, I again assume, is true for the 'in' method. However, one does run faster than the other.
Additionally, would this hold up for more conditions? E.g. when i should be checked to be one of 100 values?
Aucun commentaire:
Enregistrer un commentaire