This function makes it easier to use the timeit module from the interactive interpreter.
Just specify function with (optional) arguments to run, optional number of runs, and optional name of module (which if not specified defaults to the name of the function).
Example:
>>> timefunc.timefunc('r()', 20)
20 loops, best of 3: 6.91e+004 usec per loop
>>> timefunc.timefunc('rx()', 20, 'r')
20 loops, best of 3: 2.23e+004 usec per loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import sys
import timeit
def timefunc(func, n=3, mod=''):
"""Run func in timeit.main, n times, import from mod,
mod defaults to name of the func."""
parpos=func.find('(')
if parpos == -1:
print "You need to specify arguments or at least parentheses after the name of the function"
return -1
funcname=func[0:parpos]
if mod=='':
mod=funcname
timeit.main(['-s', 'from ' + mod + ' import ' + funcname, '-n', n, func])
|
Tags: debugging