Sunday, April 27, 2014

Measure the Running Time of a Piece of Code in Python (Timeit)

Timeit library give you the capability of measuring the run time of a piece of code or function. here is how to use it:

import timeit
start_time = timeit.default_timer()
# code you want to evaluate
print 'Hello world!'
elapsed = timeit.default_timer() - start_time
print 'Time:',elapesd


Here is the result:
Hello world!
Time: 4.19853863036e-05

No comments:

Post a Comment