2

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
DavidG
  • 24,279
  • 14
  • 89
  • 82
Dipti C
  • 35
  • 6
  • 1
    What happens if you increase your range to say... 10000000? – DavidG Mar 09 '18 at 13:56
  • You want to use `timeit` if you want to time a function, script or whatever. See [this thread](https://stackoverflow.com/questions/17579357/time-time-vs-timeit-timeit). – IMCoins Mar 09 '18 at 14:03
  • Hi David, please see output :In [55]: run numpy_practice.py enter the range10000000 time required by pythonsum is 766959 time required by numpysum is 123327 In [56]: run numpy_practice.py enter the range5 time required by pythonsum is 0 time required by numpysum is 0 In [57]: run numpy_practice.py enter the range4000 time required by pythonsum is 4812 time required by numpysum is 0 In [58]: run numpy_practice.py enter the range40 time required by pythonsum is 0 time required by numpysum is 496 – Dipti C Mar 09 '18 at 17:11
  • can you please check the last run, when I entered the range 40,time consumed by pythonsum is 0 while that of numpysum is 496. it shows numpysum is slower than pythonsum. Not able to understand the reason behind it. – Dipti C Mar 09 '18 at 17:13
  • I will point out that there are some cases where lists are faster, generally if your data is non-numeric (for example https://stackoverflow.com/questions/49112552/vectorized-string-operations-in-numpy-why-are-they-rather-slow/49134333#49134333) – user2699 Mar 09 '18 at 17:40
  • thank you so much all, will go through the post. – Dipti C Mar 10 '18 at 07:41

1 Answers1

0

Change the code bellow the boiler plate to this and use the timeit module instead. This code allows me to execute the functions a number of times and return the mean ans standard deviation execution time.

if __name__ == "__main__":
    import timeit
    n = int(input("enter the range"))
    setup = """\
import numpy as np
from __main__ import numpysum,pythonsum
n = {iterations}

    """.format(iterations=n)

    npex = numpy.array(timeit.repeat('numpysum(n)',setup=setup,repeat=100,number=1000))
    pyex = numpy.array(timeit.repeat('pythonsum(n)',setup=setup,repeat=100,number=1000))
    print("Numpy Execution Time, mean={m},std={sd}".format(m=npex.mean(),sd=npex.std(0)))
    print("Python Execution Time, mean={m},std={sd}".format(m=pyex.mean(), sd=pyex.std(0)))

By testing with n = 100, it is clear that numpy is much faster

enter the range100
Numpy Execution Time, mean=0.00482892623162404,std=0.0005752600215192671
Python Execution Time, mean=0.1037349765653833,std=0.004933817536363718
DJK
  • 8,924
  • 4
  • 24
  • 40
  • Thank you DJK, one clarification could you please give, what is meant by this like n = {iterations}. – Dipti C Mar 10 '18 at 07:43
  • @Dipti C, of course. `timeit` takes an argument setup, so if we need code to setup the code that is timed we write it in a string and pass it to time it. `.format` allows us to insert values in a string wherever there exists a `{}`, I just named the variable iterations for clarity, n does the same thing in this code as it does in your code. – DJK Mar 10 '18 at 14:02
  • @DipitiC , will you consider making it as the accepted solution? – DJK Mar 11 '18 at 01:54