Numpy is the core library for scientific computing in Python while list is the core library of Python.

As list in python are collections of heterogeneous data types stored in non-contiguous memory locations while Numpy Array are collection of homogeneous data-types that are stored in contiguous memory locations.

The NumPy Package integrates C, C++ in Python. These programming language takes less execution time as compared to Python. The benefit of using NumPy arrays over list is NumPy Arrays have smaller memory consumption and it has also better runtime behavior.

## Example: Why is NumPy faster than Lists

```
import numpy as np
import time
size = 100 #size
#creating lists
list_1 = [i for i in range(size)]
list_2 = [i for i in range(size)]
#creating arrays
array_1 = np.arange(size)
array_2 = np.arange(size)
# Concatenation
#In case of Lists
print("Concatenation:")
initial_Time = time.time()
list_1 = list_1 + list_2
# calculating time taken in execution
print("Time taken by Lists in Performing Concatenation:",
(time.time() - initial_Time),"seconds")
# In case of NumPy array
initial_Time = time.time()
array_0= np.concatenate((array_1, array_2),axis = 0)
# calculating execution time
print("Time taken by NumPy Arrays in Performing Concatenation :",
(time.time() - initial_Time),"seconds")
#Calculating Dot Product
dot =0
print("\nDot Product:")
# In case of list
initial_Time = time.time()
for a, b in zip(list_1,list_2):
dot_1 = dot + (a * b)
# calculating time taken in execution
print("Time taken by Lists in calculating Dot Product:",
(time.time() - initial_Time),"seconds")
#In case of NumPy array
initial_Time = time.time()
array=np.dot(array_1, array_2)
# calculating time taken in execution
print("Time taken by NumPy Arrays in calculating Dot Product:",
(time.time() - initial_Time),"seconds")
# Deletion
print("\nDeletion: ")
# In case of list
initial_Time = time.time()
del(list_1)
# calculating execution time
print("Time taken by Lists :",
(time.time() - initial_Time),"seconds")
# In case of NumPy Array
initial_Time = time.time()
del(array_1)
# calculating execution time
print("Time taken by NumPy Arrays :",
(time.time() - initial_Time),"seconds")
```

** Output:**

Concatenation: Time taken by Lists in Performing Concatenation: 0.00010704994201660156 seconds Time taken by NumPy Arrays in Performing Concatenation : 0.0001709461212158203 seconds Dot Product: Time taken by Lists in calculating Dot Product: 0.00017213821411132812 seconds Time taken by NumPy Arrays in calculating Dot Product: 0.00014209747314453125 seconds Deletion: Time taken by Lists : 0.00010395050048828125 seconds Time taken by NumPy Arrays : 8.988380432128906e-05 seconds

From the example, we can see that operations done on NumPy Arrays are executed faster than operation done on Python lists. The Deletion has the highest difference in execution time as compared to other operations in the example. Thus, we conclude that NumPy Array is faster than Python Lists.

- Log in to post comments