How do you calculate program run time in python? [duplicate] How do you calculate program run time in python? [duplicate] python python

How do you calculate program run time in python? [duplicate]


Quick alternative

import timeitstart = timeit.default_timer()#Your statements herestop = timeit.default_timer()print('Time: ', stop - start)  


You might want to take a look at the timeit module:

http://docs.python.org/library/timeit.html

or the profile module:

http://docs.python.org/library/profile.html

There are some additionally some nice tutorials here:

http://www.doughellmann.com/PyMOTW/profile/index.html

http://www.doughellmann.com/PyMOTW/timeit/index.html

And the time module also might come in handy, although I prefer the later two recommendations for benchmarking and profiling code performance:

http://docs.python.org/library/time.html


I don't know if this is a faster alternative, but I have another solution -

from datetime import datetimestart=datetime.now()#Statementsprint datetime.now()-start