Possible Duplicates:
Accurate timing of functions in python
accurately measure time python function takes
How can i mesure and compare the running times of my algorithms written in python .Also point me to a nice algorithms site/forum like stackoverflow if you can.
For small algorithms you can use the module timeit from python documentation:
def test():
"Stupid test function"
L = []
for i in range(100):
L.append(i)
if __name__=='__main__':
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print t.timeit()
Less accurately but still valid you can use module time like this:
from time import time
t0 = time()
call_mifuntion_vers_1()
t1 = time()
call_mifunction_vers_2()
t2 = time()
print 'function vers1 takes %f' %(t1-t0)
print 'function vers2 takes %f' %(t2-t1)
The module timeit
is useful for this and is included in the standard Python distribution.
Example:
import timeit
timeit.Timer('for i in xrange(10): oct(i)').timeit()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With