Can someone explain why I obtain such strange and different times during array allocation, it turns out that it 25x times faster to allocate slightly bigger array and slice it, than to allocate the array of needed size:
%timeit arr = np.zeros((360, 360))
207 µs ± 4.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
arr = np.zeros((362, 362))
arr = arr[:360]
8.4 µs ± 651 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Is there something general behind that or it is some Windows related issue? While this situation is specific near (360, 360) size
on my computer, I do not know if it can arise at other places.
EDIT: While this question is marked as a duplicate, the answer to that question does not not fully explain the problem:
%timeit -n10 -r10 arr = np.zeros((361, 361))
243 µs ± 56.6 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)
%timeit -n10 -r10 arr = np.zeros((362, 362))
6.82 µs ± 539 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
With zeros
there is 25-35x regression, but with empty
the story is in opposite direction with 2-5x:
%timeit -n10 -r10 arr = np.empty((361, 361))
2.49 µs ± 1.02 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)
%timeit -n10 -r10 arr = np.empty((362, 362))
11.9 µs ± 1.58 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)
Windows 7
Python 3.6.3
numpy 1.13.3