measuring a function time

Hrvoje Niksic hniksic at xemacs.org
Fri Jul 30 08:28:59 EDT 2010


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

> On Thu, 29 Jul 2010 14:42:58 +0200, Matteo Landi wrote:
>
>> This should be enough
>> 
>>>>>import time
>>>>>tic = time.time()
>>>>>function()
>>>>>toc = time.time()
>>>>>print toc - tic
>
> You're typing that in the interactive interpreter, which means the
> timer is counting the seconds while you're typing subsequent
> commands. At the very least, you need to put that code into a
> function.

Or, trivially improved as follows:

>>> t0 = time.time(); function(); t1 = time.time()
>>> print t1 - t0

This technique, while nowhere nearly as thorough as the timeit module,
still gives useful results for simple measurements.



More information about the Python-list mailing list