12.7. Comprehension Performance

12.7.1. Microbenchmark

  • 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

>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = []
... for x in range(0,5):
...     result.append(x)
...
112 ns ± 26.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
109 ns ± 26.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
108 ns ± 26.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
109 ns ± 23.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
104 ns ± 21.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = [x for x in range(0,5)]
...
103 ns ± 21.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
102 ns ± 21.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
110 ns ± 23.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
111 ns ± 26.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
111 ns ± 27.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = [x for x in range(0,50)]
...
341 ns ± 48.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
351 ns ± 59.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
356 ns ± 58.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
354 ns ± 60.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
349 ns ± 63.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = []
... for x in range(0,50):
...     result.append(x)
...
468 ns ± 60.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
461 ns ± 73.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
465 ns ± 54.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
469 ns ± 67.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
468 ns ± 61.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = [x for x in range(0,500)]
...
4.68 μs ± 213 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
4.66 μs ± 207 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
4.8 μs ± 234 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
4.62 μs ± 251 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
4.74 μs ± 265 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -r 1000 -n 1000
... result = []
... for x in range(0,500):
...     result.append(x)
...
5.91 μs ± 269 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
5.99 μs ± 301 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
5.99 μs ± 275 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
5.97 μs ± 272 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
5.98 μs ± 284 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

Conclusion:

In this case comprehensions are faster then regular loops (PEP 20).

12.7.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)