Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: How to measure execution time of a program

Reply
Thread Tools

Re: How to measure execution time of a program

 
 
Fredrik Lundh
Guest
Posts: n/a
 
      06-28-2006
Girish Sahani wrote:

> Can anyone tell me the simplest way to do it (some code snippet that
> could be included in the program's main function) ??


simplest way:

t0 = time.time()
main()
print time.time() - t0, "seconds"

(assuming that you want to measure wall time, and that your program runs
for at least a tenth of second, or so. for benchmarking of short code snippets,
see the "timeit" module)

</F>



 
Reply With Quote
 
 
 
 
Pete Forman
Guest
Posts: n/a
 
      06-28-2006
"Fredrik Lundh" <(E-Mail Removed)> writes:
> simplest way:
>
> t0 = time.time()


You can get better resolution by using time.clock() instead of
time.time().
--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent
http://www.velocityreviews.com/forums/(E-Mail Removed) -./\.- opinion of Schlumberger, Baker
http://petef.port5.com -./\.- Hughes or their divisions.

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      06-28-2006
Pete Forman wrote:

> > t0 = time.time()

>
> You can get better resolution by using time.clock() instead of
> time.time().


depends on the platform, and whether you want wall time or process time.

</F>



 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      06-28-2006
On 2006-06-28, Pete Forman <(E-Mail Removed)> wrote:
> "Fredrik Lundh" <(E-Mail Removed)> writes:
> > simplest way:
> >
> > t0 = time.time()

>
> You can get better resolution by using time.clock() instead of
> time.time().


Oh really? When I do it, time.clock() is worse:

------------------------------timeit.py------------------------------
import time

for i in range(5):
t0 = time.time()
print "hi there"
print time.time()-t0

for i in range(5):
t0 = time.clock()
print "hi there"
print time.clock()-t0
------------------------------timeit.py------------------------------

hi there
0.000149011611938
hi there
4.10079956055e-05
hi there
3.981590271e-05
hi there
3.981590271e-05
hi there
3.88622283936e-05
hi there
0.0
hi there
0.0
hi there
0.0
hi there
0.0
hi there
0.0


--
Grant Edwards grante Yow! I'm a nuclear
at submarine under the
visi.com polar ice cap and I need
a Kleenex!
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      06-28-2006
Grant Edwards wrote:

>> You can get better resolution by using time.clock() instead of
>> time.time().

>
> Oh really? When I do it, time.clock() is worse:


on Unix, time.clock() is a tick counter; if your process is running when the tick
interrupt arrives, the internal counter value is incremented (whether the process
actually used the full tick slot or not). the tick resolution is usually 1-10 milli-
seconds.

on Windows, time.clock() is a high-resolution CPU counter, with microsecond
resolution.

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
[C#] How to measure the time of your code's execution Shadow of Socrates C++ 0 01-22-2008 03:52 AM
[C#] How to measure the time of your code's execution Shadow of Socrates C Programming 0 01-22-2008 03:52 AM
Re: How to measure execution time of a program Girish Sahani Python 1 06-28-2006 09:14 AM
How to measure execution time of a program Girish Sahani Python 0 06-28-2006 07:52 AM



Advertisments