Function to show time to execute another function

Cecil Westerhof Cecil at decebal.nl
Sun Jun 7 08:58:40 EDT 2015


On Sunday  7 Jun 2015 11:28 CEST, Steven D'Aprano wrote:

> Here is a simple example:
>
> http://code.activestate.com/recipes/577896-benchmark-code-with-the-with-statement/
I use that now in my function:
#-----------------------------------------------------------------------
def time_test(function, arguments, print_time = True):
    t = Timer()
    with t:
        results     = function(*arguments)
    used_time = t.interval
    if print_time:
        print('It took {0} seconds'.format(used_time))
    else:
        results = (used_time, results)
    return results
#-----------------------------------------------------------------------

I find that neater. It also looks like the time value is a little
less, but I did not really measure it.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list