I ran the code
a = True
b = False
c = False
d = False
e = False
import time
iterations = int(1e6)
start = time.time()
for _ in range(iterations):
a or b or c or d or e
print(time.time() - start)
start = time.time()
for _ in range(iterations):
b or c or d or e or a
print(time.time() - start)
Results
0.10876178741455078
0.26296424865722656
- Why does the order of boolean evaluation make a difference in speed?
- Is it because of some form of optimization?
- If so is there a resource I can read?