A timer function

Let us make a function timer for measuring the efficiency of an arbitrary function. timer takes 4 arguments:

a function to call
a list of arguments to the function
number of calls to make (repetitions)
name of function (for printout)
def timer(func, args, repetitions, func_name):
    t0 = time.time();  c0 = time.clock()

    for i in range(repetitions):
        func(*args)  # old style: apply(func, args)

    print "%s: elapsed=%g, CPU=%g" % \
    (func_name, time.time()-t0, time.clock()-c0)

previousnexttable of contents