Nicolas P. Rougier
2016-12-26 09:34:06 UTC
Hi all,
I'm trying to understand why viewing an array as bytes before clearing makes the whole operation faster.
I imagine there is some kind of special treatment for byte arrays but I've no clue.
# Native float
Z_float = np.ones(1000000, float)
Z_int = np.ones(1000000, int)
%timeit Z_float[...] = 0
1000 loops, best of 3: 361 µs per loop
%timeit Z_int[...] = 0
1000 loops, best of 3: 366 µs per loop
%timeit Z_float.view(np.byte)[...] = 0
1000 loops, best of 3: 267 µs per loop
%timeit Z_int.view(np.byte)[...] = 0
1000 loops, best of 3: 266 µs per loop
Nicolas
I'm trying to understand why viewing an array as bytes before clearing makes the whole operation faster.
I imagine there is some kind of special treatment for byte arrays but I've no clue.
# Native float
Z_float = np.ones(1000000, float)
Z_int = np.ones(1000000, int)
%timeit Z_float[...] = 0
1000 loops, best of 3: 361 µs per loop
%timeit Z_int[...] = 0
1000 loops, best of 3: 366 µs per loop
%timeit Z_float.view(np.byte)[...] = 0
1000 loops, best of 3: 267 µs per loop
%timeit Z_int.view(np.byte)[...] = 0
1000 loops, best of 3: 266 µs per loop
Nicolas