Re: measuring a function time

2010-08-07 Thread Albert van der Horst
In article 4c5178ae$0$11091$c3e8...@news.astraweb.com,
Steven D'Aprano  st...@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.
alb...@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: measuring a function time

2010-07-30 Thread Hrvoje Niksic
Steven D'Aprano st...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: measuring a function time

2010-07-30 Thread Albert Hopkins
On Fri, 2010-07-30 at 14:28 +0200, Hrvoje Niksic wrote:
 Steven D'Aprano st...@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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: measuring a function time

2010-07-30 Thread MRAB

Albert Hopkins wrote:

On Fri, 2010-07-30 at 14:28 +0200, Hrvoje Niksic wrote:

Steven D'Aprano st...@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.
--
http://mail.python.org/mailman/listinfo/python-list


measuring a function time

2010-07-29 Thread Mahmood Naderan
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


Re: measuring a function time

2010-07-29 Thread Matteo Landi
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_mahm...@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/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: measuring a function time

2010-07-29 Thread Joe Riopel
On Thu, Jul 29, 2010 at 8:34 AM, Mahmood Naderan nt_mahm...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: measuring a function time

2010-07-29 Thread Steven D'Aprano
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: measuring a function time

2010-07-29 Thread Mahmood Naderan
Thanks, the first method worked. However clock() doesn't.

tic = time.time()
time.sleep( 2 )
toc = time.time()
print toc - tic
 
Result: 2.00269889832

Take a look at time.clock()
tic = time.clock()
time.sleep( 2 )
toc = time.clock()
print toc - tic
 
result: 0.0

More importantly, the above technique is acceptable if function() is very 
long-running (multiple seconds, at least). 
yes, the code is long running.

Thanks
// Naderan *Mahmood;





From: Matteo Landi landima...@gmail.com
To: Mahmood Naderan nt_mahm...@yahoo.com
Cc: python mailing list python-list@python.org
Sent: Thu, July 29, 2010 5:12:58 PM
Subject: Re: measuring a function time

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_mahm...@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/



  -- 
http://mail.python.org/mailman/listinfo/python-list


Re: measuring a function time

2010-07-29 Thread D'Arcy J.M. Cain
On Thu, 29 Jul 2010 08:45:23 -0400
Joe Riopel goo...@gmail.com wrote:
 On Thu, Jul 29, 2010 at 8:34 AM, Mahmood Naderan nt_mahm...@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 da...@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


Re: measuring a function time

2010-07-29 Thread Benjamin J. Racine
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 goo...@gmail.com wrote:
 On Thu, Jul 29, 2010 at 8:34 AM, Mahmood Naderan nt_mahm...@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 da...@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


Re: measuring a function time

2010-07-29 Thread Rodrick Brown
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 bjrac...@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 goo...@gmail.com wrote:
 On Thu, Jul 29, 2010 at 8:34 AM, Mahmood Naderan nt_mahm...@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 da...@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
-- 
http://mail.python.org/mailman/listinfo/python-list