Program for time Comparsion difference between Python Lists and Numpy Arrays



Creation time for adding  two python list is the order of 500 compared to the adding two numpy array. 

Time metric in seconds 

Creation time for Python List : 0.310 

Creation time for Numpy Array: 0.002



import time
import numpy as np
size = 100000

def python_method():
    t1 = time.time()
    X = range(size)
    Y = range(size)
    Z = [X[i] + Y[i] for i in range(len(X)) ]
    return time.time() - t1

def numpy_method():
    t1 = time.time()
    X = np.arange(size)
    Y = np.arange(size)
    Z = X + Y
    return time.time() - t1

t1 = python_method()
t2 = numpy_method()
print("Python",t1,"Numpy",t2)


No comments: