-
-
Notifications
You must be signed in to change notification settings - Fork 12.2k
np.zeros_like(a) is slower than np.zeros(shape=a.shape) #9909
Copy link
Copy link
Open
Description
I was comparing the time about two ways of an array of zeros using np.zeros_like and np.zeros.
I don't know if I'm missing something, but it seems that np.zeros_like is slower and, IMO, it seems there is no reason to be... Test has been made using an Jupyter Notebook, and is shown below:
import numpy as np; a=np.zeros(shape=(1000,1000))
%timeit -n100 np.zeros(shape=a.shape)
40.7 µs ± 5.72 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
import numpy as np; a=np.zeros(shape=(1000,1000))
%timeit -n100 np.zeros_like(a)
2.18 ms ± 47.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Implementation of zeros_like is here: https://github.com/numpy/numpy/blob/master/numpy/core/numeric.py#L138
This issue has been opened to suggest changing the whole implementation with
def zeros_like(a, dtype=None, order='K', subok=True):
if not subok:
return np.zeros(shape=a.shape, dtype=dtype, order=order)
else:
res = empty_like(a, dtype=dtype, order=order, subok=subok)
# needed instead of a 0 to get same result as zeros for for string dtypes
z = zeros(1, dtype=res.dtype)
multiarray.copyto(res, z, casting='unsafe')
return resReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels