dtype
s
Using the solutions to the exercise "Different arrays" as our list of arrays we find that:
np.arange(3).dtype #int64
np.linspace(0,1,3).dtype #float64
np.ones(3).dtype #float64
np.zeros((3,3)).dtype #float64
np.eye(3).dtype #float64
np.diag(np.arange(1,4)).dtype #int64
np.random.rand(3).dtype #float64
np.random.randn(3).dtype #float64
NumPy only uses int64
if it knows all the elements are integers, and even then it will often default to float64
as NumPy assumes that is what will be used.
On this machine:
from math import sqrt
def sqrt_list(list):
for i, x in enumerate(a):
a[i] = sqrt(x)
%timeit sqrt_list(list(range(100000)))
# 19 ms ± 289 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit np.sqrt(np.arange(100000))
# 636 µs ± 2.58 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
.