Created
February 20, 2014 22:18
-
-
Save davydany/9124472 to your computer and use it in GitHub Desktop.
Timeit Method Decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def timeit(func=None,loops=1,verbose=False): | |
if func != None: | |
def inner(*args,**kwargs): | |
sums = 0.0 | |
mins = 1.7976931348623157e+308 | |
maxs = 0.0 | |
print '====%s Timing====' % func.__name__ | |
for i in range(0,loops): | |
t0 = time.time() | |
result = func(*args,**kwargs) | |
dt = time.time() - t0 | |
mins = dt if dt < mins else mins | |
maxs = dt if dt > maxs else maxs | |
sums += dt | |
if verbose == True: | |
print '\t%r ran in %2.9f sec on run %s' %(func.__name__,dt,i) | |
print '%r min run time was %2.9f sec' % (func.__name__,mins) | |
print '%r max run time was %2.9f sec' % (func.__name__,maxs) | |
print '%r avg run time was %2.9f sec in %s runs' % (func.__name__,sums/loops,loops) | |
print '==== end ====' | |
return result | |
return inner | |
else: | |
def partial_inner(func): | |
return timeit(func,loops,verbose) | |
return partial_inner | |
@timeit(loops=1000) | |
def myTestFunction(): | |
# call cpu intensive task | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not my code. Found it on the internet somewhere and have been using it.