w3resource

NumPy: Test whether numpy array is faster than Python list or not

NumPy: Array Object Exercise-193 with Solution

Write a Numpy program to test whether numpy array is faster than Python list or not.

Sample Solution:

Python Code:

 # Importing necessary libraries
import time  # Importing time module for time-related functionalities
import numpy as np  # Importing NumPy library

# Defining the size of the arrays
SIZE = 200000

# Creating lists and NumPy arrays of integers from 0 to SIZE - 1
list1 = range(SIZE)
list2 = range(SIZE)
arra1 = np.arange(SIZE)
arra2 = np.arange(SIZE)

# Measuring the time taken to aggregate elements from each iterable using a list comprehension
start_list = time.time()  # Marking the start time
result = [(x, y) for x, y in zip(list1, list2)]  # Aggregating elements from 'list1' and 'list2' using list comprehension
print("Time to aggregate elements from each of the iterables:")
print("List:")
print((time.time() - start_list) * 1000)  # Printing the execution time in milliseconds

# Measuring the time taken to add NumPy arrays element-wise
start_array = time.time()  # Marking the start time
result = arra1 + arra2  # Performing element-wise addition on 'arra1' and 'arra2' using NumPy
print("NumPy array:")
print((time.time() - start_array) * 1000)  # Printing the execution time in milliseconds

Sample Output:

Time to aggregates elements from each of the iterables:
List:
72.64399528503418
NumPy array:
19.61684226989746

Explanation:

In the above code -

  • SIZE: A constant integer variable with the value of 200000, representing the size of the lists and arrays.
  • list1 and list2: Two Python lists created using the range function, each containing integers from 0 to SIZE-1.
  • arra1 and arra2: Two NumPy arrays created using the np.arange function, each containing integers from 0 to SIZE-1.
  • start_list: Records the current time before the list element-wise addition starts.
  • result: A list comprehension with zip is used to add list1 and list2 element-wise. It creates a list of tuples containing the sum of corresponding elements from list1 and list2.
  • print((time.time()-start_list)*1000): This line calculates the time taken for the list addition operation in milliseconds and prints it.
  • start_array: Records the current time before the NumPy array element-wise addition starts.
  • result = arra1 + arra2: This line adds the two NumPy arrays arra1 and arra2 element-wise directly.
  • print((time.time()-start_array)*1000): Calculates the time taken for the NumPy array addition operation in milliseconds and prints it.

Python-Numpy Code Editor:

Previous: Write a NumPy program to extract all the contiguous 4x4 blocks from a given random 12x12 matrix.
Next: Write a NumPy program to create two arrays with shape (300,400, 5), fill values using unsigned integer (0 to 255). Insert a new axis that will appear at the beginning in the expanded array shape. Now combine the said two arrays into one.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.