Re: How to measure execution time of a program

F

Fredrik Lundh

Girish said:
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>
 
P

Pete Forman

Fredrik Lundh said:
> 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
(e-mail address 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
 
F

Fredrik Lundh

Pete said:
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>
 
G

Grant Edwards

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
 
F

Fredrik Lundh

Grant said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top