Custom Search

Tuesday, April 26, 2011

how to measure execution time of functions using decorator

how to measure execution time of functions using decorator

from time import time

#decorator def
def timed(f):
def wrapper(*args, **kwds):
start = time()
result = f(*args, **kwds)
elapsed = time() - start
print "%s took %d time to finish" % (f.__name__, elapsed)
return result
return wrapper


#function def
@timed
def test():
for i in range(1,20000000):
a=0



#call
test()


OUTPUT
========
test took 1 time to finish

No comments:

Post a Comment