Strange behavior when allocating an array in numpy

Can someone explain why I get such strange and different times when allocating an array, it turns out that it is 25x faster to select a slightly larger array and cut it than to select an array of the right 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 in common behind, or is it a Windows related issue? Although this situation is especially close to the (360, 360) size on my computer, I don’t know if it can occur elsewhere.

EDIT: While this question is marked as duplicate, the answer to this question cannot 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 a 25-35x regression, but with empty story in the opposite direction is 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

+5
source share

Source: https://habr.com/ru/post/1274257/


All Articles