Python computation speed....Once again...

  • 0
Just come across this video on youtube.
I was amazed by the difference in computation time between C++ and Python. I tried to do the same in my PC and compared with what I can obtain using the numpy library. Below the result.

#! /usr/bin/python2.7
from datetime import datetime

flips = 4000000

def regularPython():
    import random
    heads = 0

    for i in range(flips):
        if random.randint(0,1) == 0:
            heads+=1

    tails = flips-heads
    print 'heads=',heads
    print 'tails=',tails

def numpyVersion1():
    import numpy as np
    heads = 0

    for i in range(flips):
        if np.random.random_integers(0,1) == 0:
            heads+=1

    tails = flips-heads
    print 'heads=',heads
    print 'tails=',tails

def numpyVersion2():
    import numpy as np
    
    randomArray = np.random.random_integers(0,1,flips)

    heads = len(randomArray[randomArray==0])

    tails = flips-heads
    print 'heads=',heads
    print 'tails=',tails


if __name__=="__main__":
    t_start = datetime.now()
    regularPython()
    t_end = datetime.now()
    print "Total time: ", (t_end-t_start)
    t_start = datetime.now()
    numpyVersion1()
    t_end = datetime.now()
    print "Total time: ", (t_end-t_start)
    numpyVersion2()
    t_end = datetime.now()
    print "Total time: ", (t_end-t_start)


Here the output:

heads= 2001161
tails= 1998839
Total time:  0:00:06.139672
heads= 1999621
tails= 2000379
Total time:  0:00:01.597668
heads= 1999881
tails= 2000119
Total time:  0:00:01.707101


No comments:

Post a Comment