%timeit
isn't supposed to work correctly inside functions (currently). If you start a fresh notebook (or restart yours) and only use:
import numpy as np
def my_func(n):
for i in range(n):
arr = np.random.rand(2**(i+10))
%timeit -n 10 np.sort(arr)
my_func(10)
It will throw a NameError
:
NameError: name 'arr' is not defined
That's because %timeit
only inspects the global variables not the local ones (so it ignores the variable arr = np.random.rand(2**(i+10))
defined inside your function).
If you use this code it will be obvious:
import numpy as np
arr = np.array([1, 2, 3])
def my_func(n):
for i in range(n):
arr = np.random.rand(2**(i+10))
%timeit -n 2 -r 1 print(arr)
my_func(10)
which prints:
[1 2 3]
[1 2 3]
3.44 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
[1 2 3]
[1 2 3]
670 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
[1 2 3]
[1 2 3]
2.04 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
[1 2 3]
[1 2 3]
451 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
[1 2 3]
[1 2 3]
906 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
[1 2 3]
[1 2 3]
1.01 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
[1 2 3]
[1 2 3]
767 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
[1 2 3]
[1 2 3]
890 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
[1 2 3]
[1 2 3]
1.28 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
[1 2 3]
[1 2 3]
919 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)
So in your case it always found the last arr
from your non-function runs (which was global). Which also explains why the time was roughly identical for the function. Because it always found the same arr
.