Why do we need NumPy arrays when we have lists in Python?
There are two main reasons why we would use NumPy array instead of lists in Python. These reasons are:
- Less memory usage: The Python NumPy array consumes less memory than lists.
- Less execution time: The NumPy array is pretty fast in terms of execution, as compared to lists in Python.
Now we’ll look at the proof for what I have stated above.
Less memory usage
In the code snippets below we will see the memory usage for lists and NumPy array.
import syslist_array = [i for i in range(100)]print("Length of List array: ",len(list_array))print("Type of array: ",type(list_array))print("Memory consumption by an element in the array: ",sys.getsizeof(list_array[0]))print("Memory usage by list array: ",(sys.getsizeof(list_array[0]))*len(list_array))
Explanation:
- On line 1, we import the
syspackage to get the size of our array later in the code. - On line 3, we created a list array
list_arraythat contains 100 elements. - On line 5, we print the length of our
list_array. - On line 7, we print the type of the array.
- On line 9, we calculate the size of each element present in the array. Notice that each element takes 24-bit in List array.
- Finally, on line 12, we calculate the whole size of the List array, which comes out to be 2400 bit.
Now, let’s see the NumPy array’s memory usage.
import numpy as npnumpy_array = np.arange(100)print("Length of NumPy array: ",len(numpy_array))print("Type of array: ",type(numpy_array))print("Memory consumption by an element in the array: ",numpy_array.itemsize)print("Memory usage by list array: ",numpy_array.itemsize*numpy_array.size)
Explanation
- On line 1, we import the
numpypackage to create an array. - On line 3, we created a numpy array
numpy_arraythat contains 100 elements. - On line 5, we print the length of our
numpy_array. - On line 7, we print the type of the array.
- On line 9, we calculate the size of each element present in the array. Notice that each element takes 8-bits in NumPy array.
- Finally, on line 12, we calculate the whole size of the NumPy array, which comes out to be 800 bits.
So, we can conclude that the first reason why we need NumPy arrays is because its memory consumption is far less than that of List arrays.
Less execution time
Now, let’s take at look at the execution time difference in the case of List arrays and NumPy arrays.
import timeimport numpy as nplist_array1 = [i for i in range(1000000)]list_array2 = [i for i in range(1000000)]start_time = time.time()list_multiply = [list_array1[i] * list_array2[i]for i in range(1000000)]final_time = time.time()print("Time taken by Lists to perform multiplication: ",(round((final_time - start_time), 2)), "millisecond")numpy_array1 = np.arange(1000000)numpy_array2 = np.arange(1000000)start_time = time.time()numpy_multiply = numpy_array1 * numpy_array2final_time = time.time()print("Time taken by Lists to perform multiplication: ",(round((final_time - start_time), 2)), "millisecond")
Explanation
-
From lines 4 to 13, we created two List arrays and then performed multiplication between those two arrays. Then, we calculated the execution time.
-
Similarly from lines 15 to 23, we created two NumPy arrays and performed multiplication between them. Then, we calculated the execution time.
So, we can conclude that the second reason why we need NumPy arrays is because it took less time to complete its execution than the List arrays.