From: Matteo Landi on
This should be enough

>>>import time
>>>tic = time.time()
>>>function()
>>>toc = time.time()
>>>print toc - tic

On Thu, Jul 29, 2010 at 2:34 PM, Mahmood Naderan <nt_mahmood(a)yahoo.com> wrote:
> Hi,
> I want to measure a function run time. I read
> http://docs.python.org/library/time.html but I am confused and don't know
> which one is suitable. I don't know is daylight timing important or not.....
> or is Y2K issue important for my case or not.... I also don't know how epoch
> time is related to my work.
>
> I just want to do this (pseudocode):
> start_time = get_current_time;
> function();
> end_time = get_current_time;
> print (end_time - start_time)
>
> the output should be 7600 (s) for example. What is the best and easiest way
> to do that?
>
> Thanks,
>
> // Naderan *Mahmood;
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



--
Matteo Landi
http://www.matteolandi.net/
From: Joe Riopel on
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()

http://docs.python.org/library/time.html#time.clock

"this is the function to use for benchmarking Python or timing algorithms."
From: Steven D'Aprano on
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
From: D'Arcy J.M. Cain on
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.
From: Benjamin J. Racine on
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