5.4. Case Study: Getitem

  • Date: 2024-06-03

  • Python: 3.12.3

  • IPython: 8.25.0

  • System: macOS 14.5

  • Computer: MacBook M3 Max

  • CPU: 16 cores (12 performance and 4 efficiency) / 3nm

  • RAM: 128 GB RAM LPDDR5

5.4.1. SetUp

>>> DATA = [
...     (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'),
... ]

5.4.2. Solution 1

>>> 
... %%timeit -r1000 -n1000
... for row in DATA:
...     values = row[0:4]
...     species = row[4]

Results:

  • 129 ns ± 22.0 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 136 ns ± 31.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 134 ns ± 28.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 133 ns ± 28.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 134 ns ± 30.0 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

5.4.3. Solution 2

>>> 
... %%timeit -r1000 -n1000
... for row in DATA:
...     values = row[:4]
...     species = row[4]

Results:

  • 118 ns ± 26.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 119 ns ± 24.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 119 ns ± 25.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 120 ns ± 27.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 117 ns ± 22.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

5.4.4. Solution 3

>>> 
... %%timeit -r1000 -n1000
... for row in DATA:
...     values = row[0:-1]
...     species = row[-1]

Results:

  • 153 ns ± 32.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 154 ns ± 34.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 154 ns ± 34.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 154 ns ± 35.0 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 153 ns ± 36.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

5.4.5. Solution 4

>>> 
... %%timeit -r1000 -n1000
... for row in DATA:
...     values = row[:-1]
...     species = row[-1]

Results:

  • 143 ns ± 29.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 143 ns ± 28.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 142 ns ± 26.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 143 ns ± 30.1 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 144 ns ± 31.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

5.4.6. Solution 5

>>> 
... %%timeit -r1000 -n1000
... for row in DATA:
...     *values, species = row

Results:

  • 220 ns ± 43.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 220 ns ± 42.1 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 219 ns ± 40.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 223 ns ± 45.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 220 ns ± 43.0 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

5.4.7. Solution 6

>>> 
... %%timeit -r1000 -n1000
... for *values, species in DATA:
...     pass

Results:

  • 218 ns ± 42.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 219 ns ± 45.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 220 ns ± 41.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 217 ns ± 40.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)

  • 208 ns ± 14.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)