Created
June 8, 2009 23:47
-
-
Save fredrik/126145 to your computer and use it in GitHub Desktop.
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 | |
# decorator that measures execution time of a function. | |
def timing(func): | |
def wrapper(*arg): | |
t0 = time.time() | |
r = func(*arg) | |
elapsed = time.time() - t0 | |
print '%s => %0.3f ms' % (func.func_name, elapsed*1000.00) | |
return r | |
return wrapper | |
# decorator that measures the average execution time of a hundred function calls. | |
def timingx100(func): | |
n = 1000 | |
def avg_wrapper(*arg): | |
tot = 0 | |
emax = 0 # intmax | |
emin = 0 # and intmin? | |
for i in range(1, n): | |
t0 = time.time() | |
r = func(*arg) | |
elapsed = time.time() - t0 | |
print '%s%s => %0.3f ms' % (func.func_name, arg, elapsed*1000.00) | |
tot += elapsed | |
if elapsed > emax or emax == 0: | |
emax = elapsed | |
if elapsed < emin or emin == 0: | |
emin = elapsed | |
avg = tot / n | |
print 'average> %s => %0.3f ms' % (func.func_name, avg*1000.00) | |
print 'min: %0.3f ms max: %0.3f ms' % (emin*1000.00, emax*1000.00) | |
# TODO: stddev, etc | |
return avg_wrapper | |
@timingx100 | |
def f(xs=[]): | |
sum = 0 | |
for x in xs: | |
sum += x | |
return sum | |
if __name__ == '__main__': | |
f([4,8, 16]*1024) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment