5.6. Case Study: Comprehensions
5.6.1. Microbenchmark
>>>
... %%timeit -r 1000 -n 1000
... result = []
... for x in range(0,5):
... result.append(x)
...
457 ns ± 69.4 ns per loop (mean ± std. dev. of 1000 runs, 1000 loops each)
>>>
... %%timeit -r 1000 -n 1000
... result = [x for x in range(0,5)]
...
411 ns ± 76.6 ns per loop (mean ± std. dev. of 1000 runs, 1000 loops each)
>>>
... %%timeit -r 1000 -n 1000
... result = [x for x in range(0,50)]
...
1.45 µs ± 181 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>>
... %%timeit -r 1000 -n 1000
... result = []
... for x in range(0,50):
... result.append(x)
...
2.79 µs ± 306 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>>
... %%timeit -r 1000 -n 1000
... result = [x for x in range(0,500)]
...
14.1 µs ± 1.02 µs per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>>
... %%timeit -r 1000 -n 1000
... result = []
... for x in range(0,500):
... result.append(x)
...
28.5 µs ± 2.23 µs per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
Conclusion:
In this case comprehensions are twice as fast as regular loops (PEP 20).
5.6.2. Performance
Date: 2024-12-04
Python: 3.13.0
IPython: 8.30.0
System: macOS 15.1.1
Computer: MacBook M3 Max
CPU: 16 cores (12 performance and 4 efficiency) / 3nm
RAM: 128 GB RAM LPDDR5
Setup:
>>> DATA = [
... ('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
... (5.8, 2.7, 5.1, 1.9, 'virginica'),
... (5.1, 3.5, 1.4, 0.2, 'setosa'),
... (5.7, 2.8, 4.1, 1.3, 'versicolor'),
... (6.3, 2.9, 5.6, 1.8, 'virginica'),
... (6.4, 3.2, 4.5, 1.5, 'versicolor'),
... (4.7, 3.2, 1.3, 0.2, 'setosa'),
... (7.0, 3.2, 4.7, 1.4, 'versicolor'),
... ]
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = []
... for row in DATA[1:]:
... for value in row:
... if isinstance(value, float):
... result.append(value >= 1.0)
... result = all(result)
...
964 ns ± 85.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
925 ns ± 91.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
919 ns ± 52.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
941 ns ± 86.1 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
936 ns ± 77.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = True
... for row in DATA[1:]:
... for value in row:
... if isinstance(value, float):
... if not value >= 1.0:
... result = False
... break
... if not result:
... break
...
310 ns ± 47.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
306 ns ± 44.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
302 ns ± 33.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
307 ns ± 51.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
308 ns ± 57.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = all(value >= 1.0
... for row in DATA[1:]
... for value in row
... if isinstance(value, float))
...
386 ns ± 40.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
388 ns ± 58.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 52.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
388 ns ± 52.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
385 ns ± 57.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = all(value >= 1.0 for row in DATA[1:] for value in row if isinstance(value, float))
...
387 ns ± 63.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
389 ns ± 61 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 55.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
384 ns ± 61 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 64.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = all(y >= 1.0 for x in DATA[1:] for y in x if isinstance(y, float))
...
387 ns ± 63.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 65.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
388 ns ± 63.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
386 ns ± 58.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
385 ns ± 62.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = all(x >= 1.0 for X in DATA[1:] for x in X if isinstance(x, float))
...
389 ns ± 60.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
388 ns ± 64.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 60.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
386 ns ± 63.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 65.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)