Function to show time to execute another function
Mark Lawrence
breamoreboy at yahoo.co.uk
Sun Jun 7 03:39:20 EDT 2015
On 07/06/2015 07:39, Cecil Westerhof wrote:
> Sometimes I just want to know how much time a function takes, but at
> the same time I also want the result of the function. For this I wrote
> the following function:
> def time_test(function, *args):
> startTime = time.time()
> results = function(*args)
> endTime = time.time()
> print('It took {0} seconds'.format(endTime - startTime))
> return results
>
> I can do:
> time_test(test_random, 100, 10 ** 5)
> This outputs:
> It took 17.01685857772827 seconds
> and returns:
> (98592, 100833, 0.977775133140936)
>
> When executing:
> time_test(test_random, 100, 10 ** 6)
> it outputs:
> It took 165.26371836662292 seconds
> and returns:
> (997103, 1002009, 0.9951038363926871)
>
https://docs.python.org/3/library/timeit.html
https://docs.python.org/3/library/profile.html
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Python-list
mailing list