I am trying to compare the processing time for python list and numpy array. I know the fact that numpy arrays are faster than the python list but when I am checking practically I get lists are faster than numpy array.
Here is my code :
import numpy
from datetime import datetime
def pythonsum(n):
'''
This function calculates the sum of two python list
'''
a = list(range(n))
b = list(range(n))
c = []
total = 0
for i in range(len(a)):
a[i] = i ** 2
b[i] = i ** 3
c.append(a[i] + b[i])
return c
def numpysum(n):
'''
This function calculates the sum of two numpy array
'''
a = numpy.arange(n) ** 2
b = numpy.arange(n) ** 3
c = a + b
return c
if __name__ == "__main__":
n = int(input("enter the range"))
start = datetime.now()
result = pythonsum(n)
delta = datetime.now()-start
print("time required by pythonsum is",delta.microseconds)
start = datetime.now()
result1 = numpysum(n)
delta = datetime.now()-start
print("time required by numpysum is",delta.microseconds)
delta = datetime.now()-start
print("time required by numpysum is",delta.microseconds)
Output :
In [32]: run numpy_practice.py
enter the range7
time required by pythonsum is 0
time required by numpysum is 1001