[Solved] Why numpy arrays are slower than lists with for loops?


np.arrays are much faster when using vectorized functions or to say it another way, specifically when you are not iterating over the elements.

Here is an example. We will add 1 to each number in the ordered list you created.

The setup is similar except I use lists instead of ranges so I can operate on the values.

length = 150000000
my_list = list(range(length))
my_array = np.array(my_list)

Using the %%timeit magic function in jupyter notebooks

%%timeit
for i in range(len(my_list)):
    my_list[i] += 1

gives

38.6 s ± 3.71 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

thats about 38 seconds per loop

On the other hand

%%timeit
new_array = my_array + 1

gives

772 ms ± 58.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

The operation on the numpy array took under one second (~772ms) per loop while iterating through the list and adding one to each element too around 36 seconds per loop.

2

solved Why numpy arrays are slower than lists with for loops?