From: Rodrick Brown on
Someone should port Perl's Benchmark.pm module to python that's such a useful module to measure a functions execution time and CPU usage.

Sent from my iPhone 4.

On Jul 29, 2010, at 3:43 PM, "Benjamin J. Racine" <bjracine(a)glosten.com> wrote:

> I just use ipython's functions (that are themselves just calls to the time module functions) for timing my functions...
>
> Enter:
> %timeit?
> or
> %time
>
> At the Ipython command prompt to get started.
>
> Ben R.
>
> On Jul 29, 2010, at 7:43 AM, D'Arcy J.M. Cain wrote:
>
>> On Thu, 29 Jul 2010 08:45:23 -0400
>> Joe Riopel <goon12(a)gmail.com> wrote:
>>> On Thu, Jul 29, 2010 at 8:34 AM, Mahmood Naderan <nt_mahmood(a)yahoo.com> wrote:
>>>> the output should be 7600 (s) for example. What is the best and easiest way
>>>> to do that?
>>>
>>> Take a look at time.clock()
>>
>> I don't know if that's what he wants. The clock() method returns
>> processor time, not wall time.
>>
>> Python 2.6.5 (r265:79063, Jul 8 2010, 16:01:18)
>> [GCC 4.1.3 20080704 prerelease (NetBSD nb2 20081120)] on netbsd5
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> from time import time, clock, sleep
>>>>> t = time()
>>>>> print time() - t, clock()
>> 0.000596046447754 0.03
>>>>> sleep(3)
>>>>> print time() - t, clock()
>> 3.03474903107 0.03
>>>>> x = open("BIGFILE").read()
>>>>> print time() - t, clock()
>> 10.2008538246 1.42
>>
>> --
>> D'Arcy J.M. Cain <darcy(a)druid.net> | Democracy is three wolves
>> http://www.druid.net/darcy/ | and a sheep voting on
>> +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
> --
> http://mail.python.org/mailman/listinfo/python-list
From: Hrvoje Niksic on
Steven D'Aprano <steve(a)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.
From: Albert Hopkins on
On Fri, 2010-07-30 at 14:28 +0200, Hrvoje Niksic wrote:
> Steven D'Aprano <steve(a)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

I'll just throw this out. I sometimes use a decorator to keep track of
a functions execution times:


def timed_function(f):
"""Function decorator that records the execution time of a
function"""
import time
def funct(*args, **kwargs):
__starttime = time.time()
result = f(*args, **kwargs)
__endtime = time.time()
funct.runtime = __endtime - __starttime

return result
return funct

Then

>>> from goodies import timed_function
>>> from time import sleep
>>> @timed_function
... def test(n):
... sleep(n)
...
>>> test(4)
>>> test.runtime
4.003864049911499

Works for simple stuff anyway.

-a

From: MRAB on
Albert Hopkins wrote:
> On Fri, 2010-07-30 at 14:28 +0200, Hrvoje Niksic wrote:
>> Steven D'Aprano <steve(a)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
>
> I'll just throw this out. I sometimes use a decorator to keep track of
> a functions execution times:
>
>
> def timed_function(f):
> """Function decorator that records the execution time of a
> function"""
> import time
> def funct(*args, **kwargs):
> __starttime = time.time()
> result = f(*args, **kwargs)
> __endtime = time.time()
> funct.runtime = __endtime - __starttime
>
> return result
> return funct
>
> Then
>
> >>> from goodies import timed_function
> >>> from time import sleep
> >>> @timed_function
> ... def test(n):
> ... sleep(n)
> ...
> >>> test(4)
> >>> test.runtime
> 4.003864049911499
>
> Works for simple stuff anyway.
>
That won't work very well for functions which don't run for long. You
could fix that by adding a counter for the number of times it's run and
the total time.
From: Albert van der Horst on
In article <4c5178ae$0$11091$c3e8da3(a)news.astraweb.com>,
Steven D'Aprano <steve(a)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(a)spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst