measuring a function time
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Thu Jul 29 08:48:46 EDT 2010
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.
More importantly, the above technique is acceptable if function() is very
long-running (multiple seconds, at least). If it is quick, or a code-
snippet, the time you measure will be dominated by random chance.
The best way to time small code snippets and fast functions is with the
timeit module.
--
Steven
More information about the Python-list
mailing list