2024-heraklion-data/exercises/.ipynb_checkpoints/slow_when_slicing_wrongly-checkpoint.ipynb
2024-08-27 15:27:53 +03:00

3 KiB

In [1]:
import numpy as np
In [2]:
def get_slice_and_perform_something(x, axis):
    idx = 75
    if axis == 0:
        slice_ = x[idx, :]
    else:
        slice_ =  x[:, idx]
    # Here I divide by two but any other operation will do, 
    # we just want to simulate the fact that we actually need to read the memory
    return slice_ // 2
In [3]:
page_size = 4096
n = 100
x = np.empty((page_size * n, page_size * n), dtype='int8')
In [4]:
x.shape
Out[4]:
(409600, 409600)
In [5]:
%timeit get_slice_and_perform_something(x, axis=0)
28.2 µs ± 1.22 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [6]:
%timeit get_slice_and_perform_something(x, axis=1)
The slowest run took 5.29 times longer than the fastest. This could mean that an intermediate result is being cached.
886 ms ± 337 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [10]:
886000 / 27.5
Out[10]:
32218.18181818182
In [ ]: