casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
benchmark.py
Go to the documentation of this file.
00001 """
00002 Using tools with timeit seems to be a real pain, so this is a simpler
00003 alternative, although likely a little less accurate.
00004 """
00005 
00006 import time
00007 
00008 def benchmark(func, args, kwargs, nreps=5, nperrep=1):
00009     """
00010     Run func(*args, **kwargs) nreps times and report how much CPU time it took.
00011     
00012     args: a tuple of the positional parameters to pass to func.  Remember that
00013           (single_item) doesn't cut it - use (single_item,) as in
00014           benchmark(listvis, (vis,), 7).  (When benchmarking interactive cases
00015           like that, just hit the keys when required.)
00016 
00017     kwargs: a dictionary of keyword arguments to pass to func.
00018 
00019     nreps: should be a small integer > 1 to combat random error from other
00020            processes on the system (you probably want the minimum time).
00021 
00022     nperrep: integer >= 1 used to improve the timing precision on fast
00023              funcs, i.e. nperrep times will be added and then divided by nperrep.
00024     """
00025     meantime = 0.0
00026     maxtime =  0.0
00027     print "Run",
00028     for i in xrange(nreps):
00029         print i + 1,
00030         sys.stdout.flush()
00031         t0 = time.time()
00032         for j in xrange(nperrep):
00033             dummy = func(*args, **kwargs)
00034         wallclocktime = (time.time() - t0) / float(nperrep)
00035         meantime += (wallclocktime - meantime) / (i + 1.0)
00036         if i < 1:
00037             mintime = wallclocktime
00038         else:
00039             mintime = min(mintime, wallclocktime)
00040         maxtime = max(maxtime, wallclocktime)
00041     if nreps > 1:
00042         print "\nMin wall clock time:  %.3gs" % mintime
00043         print "Mean wall clock time: %.3gs" % meantime
00044         print "Max wall clock time:  %.3gs" % maxtime
00045     else:
00046         print "\nWall clock time: %.3gs" % mintime