With r = range(1500, 2500)
, I benchmarked x in r
for x
below/in/above the range:
1000 in r : 58 ns ± 0 ns
2000 in r : 101 ns ± 1 ns
3000 in r : 58 ns ± 0 ns
Checking 1000 is faster than checking 2000? Makes sense, as for 1000, Python knows the result after only checking the range's lower bound, whereas for 2000 it needs to check both bounds.
Checking 3000 is faster than checking 2000? Makes sense, as for 3000, Python knows the result after only checking the range's upper bound, whereas for 2000 it needs to check both bounds.
Hey... waaaiiit a minute...
How does it know which bound to check first? How can 1000 and 3000 both be checked faster than 2000?
Benchmark code (Try it online!):
from timeit import repeat
from statistics import mean, stdev
setup = 'r = range(1500, 2500)'
n = 10**4
for _ in range(3):
for x in 1000, 2000, 3000:
code = f'{x} in r'
ts = repeat(code, setup, number=n, repeat=100)
ts = [t/n * 1e9 for t in sorted(ts)[:10]]
print(code, ': %3d ns ± %d ns' % (mean(ts), stdev(ts)))
print()