Using tools with timeit seems to be a real pain, so this is a simpler
alternative, although likely a little less accurate.
def benchmark(func, args, kwargs, nreps=5, nperrep=1):
Run func(*args, **kwargs) nreps times and report how much CPU time it took.
args: a tuple of the positional parameters to pass to func. Remember that
(single_item) doesn't cut it - use (single_item,) as in
benchmark(listvis, (vis,), 7). (When benchmarking interactive cases
like that, just hit the keys when required.)
kwargs: a dictionary of keyword arguments to pass to func.
nreps: should be a small integer > 1 to combat random error from other
processes on the system (you probably want the minimum time).
nperrep: integer >= 1 used to improve the timing precision on fast
funcs, i.e. nperrep times will be added and then divided by nperrep.
for j in xrange(nperrep):
dummy = func(*args, **kwargs)
wallclocktime = (time.time() - t0) / float(nperrep)
meantime += (wallclocktime - meantime) / (i + 1.0)
mintime = min(mintime, wallclocktime)
maxtime = max(maxtime, wallclocktime)
print "\nMin wall clock time: %.3gs" % mintime
print "Mean wall clock time: %.3gs" % meantime
print "Max wall clock time: %.3gs" % maxtime
print "\nWall clock time: %.3gs" % mintime