How could i get execution times ?

Ken Seehof kseehof at neuralintegrator.com
Sat Jun 8 20:18:25 EDT 2002


> I'm looking for a way to get execution time at different points 
> of a program, by now i use the
> code below, but i've seen in some post that time.clock() could be 
> a better way to measure this,
> 
> So my question, is how you pythoners do it ??? and to understand _why_ ?

>>> for x in range(10):
...    print time.time()
    
1023578958.14
1023578958.14
1023578958.14
1023578958.14
1023578958.14
1023578958.14
1023578958.14
1023578958.14
1023578958.14
1023578958.14

>>> for x in range(10):
...    print time.clock()
    
88.9835171558
88.9849477866
88.9862024171
88.9876648955
88.9889245546
88.9901624231
88.9913986155
88.9926339697
88.9938701621
88.9951130592

>>> for x in range(10):
...    time.sleep(.01)
...    print time.time()
    
1023579557.1
1023579557.1
1023579557.16
1023579557.16
1023579557.16
1023579557.16
1023579557.16
1023579557.21
1023579557.21
1023579557.21

>>> pprint.pprint([time.clock() for i in range(10)])
[12.151166630349151,
 12.151180877989908,
 12.151191773244607,
 12.151202668499304,
 12.151213563754002,
 12.151224459008699,
 12.151235354263397,
 12.151246249518094,
 12.151258820965822,
 12.15126971622052]

In other words, time.clock() is far more precise for elapsed
time (the above example demonstrates precision on the order
of microseconds -- more than adequate for profiling python
code).  But time.time() apparently has a granularity on the
order of a tenth of a second (on windows), which is fine for
profiling slower applications like Microsoft Outlook :-)

I believe that the difference between the corresponding C
library functions is due to there being two different clocks
in the hardware.  One clock is very precise, and drives the CPU
but only knows about elapsed time, (this clock is only on when
the CPU is on).  The other clock is essential a digital watch
plugged into your computer (it runs on a battery, just like a
watch, when the CPU is off).

I hope this answers your questions.

- Ken Seehof







More information about the Python-list mailing list