Sorry if this question is a little too basic.
I was learning NumPy and learned through Why are NumPy arrays so fast? that NumPy arrays are fast because of two things
np.array() are composed of the same dtype and hence get the benefits of locality of reference.
Vectorized operations are possible via things like uFunc.
Therefore, it stands to reason that the computation speed should be:
(uFuncs with np arrays)>(loops with np arrays)>(loops with list).
However, the code below showed that the speeds were actually,
(uFuncs with np arrays)>(loops with list)>(loops with np arrays).
in other words, although using uFUncs with np arrays is the fastest, loops with lists were faster than loops with np arrays. Could anyone explain why this is so?
Thank you in advance for your answer!
import numpy as np
np.random.seed(0)
arr=np.arange(1,1000)
arrlist=[]
for i in range(1,1000):
arrlist.append(i)
#case0: list loop
def list_loop(values): #values: 1D array I want to take reciprocal of
output=[]
for i in range(len(values)):
output.append(1.0/values[i])
return output
#case1: np array loop
def nparray_loop(values): #values: 1D array I want to take reciprocal of
output=np.empty(len(values))
for i in range(len(values)):
output[i]=1.0/values[i]
return output
#case2: uFunc
def nparray_uFunc(values):
return 1.0/values
print("list loop computation time:")
%timeit list_loop(arrlist)
print('\n')
print("np array loop computation time:")
%timeit nparray_loop(arr)
print('\n')
print("uFunc computation time:")
%timeit nparray_uFunc(arr)
Output:
list loop computation time:
185 µs ± 5.63 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
np array loop computation time:
4.28 ms ± 402 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
uFunc computation time:
5.42 µs ± 1.23 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)