measuring a function time

Albert van der Horst albert at spenarnc.xs4all.nl
Sat Aug 7 13:02:54 EDT 2010


In article <4c5178ae$0$11091$c3e8da3 at news.astraweb.com>,
Steven D'Aprano  <steve at REMOVE-THIS-cybersource.com.au> wrote:
>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.

There is an alternative, for once the semicolon comes in handy:

>>>from time import *
>>> x=time(); function(); print time()-x

6.551980773430

>
>The best way to time small code snippets and fast functions is with the
>timeit module.
>


>--
>Steven

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst




More information about the Python-list mailing list