I recently timed several different array allocation and initialization procedures in numpy. I however fail to interpret these timings. Here is a plot of my measurements (size of array in number of elements “n” for a given data type vs time of execution).
If you want to reproduce the timings on your own machine, here is the code: https://gabrielfougeron.github.io/pyquickbench/_build/auto_examples/benchmarks/numpy_array_init.html#sphx-glr-build-auto-examples-benchmarks-numpy-array-init-py
In particular, here are my questions:
- Why do the timings of
np.ones
andnp.zeros
differ so much, especially considering how similar their definitions are (cf https://github.com/numpy/numpy/blob/main/numpy/matlib.py)
def ones(shape, dtype=None, order='C'):
a = ndarray.__new__(matrix, shape, dtype, order=order)
a.fill(1)
return a
def zeros(shape, dtype=None, order='C'):
a = ndarray.__new__(matrix, shape, dtype, order=order)
a.fill(0)
return a
- Why is there a discontinuity around the few million elements mark?