Precise timings ?

Fernando Perez fperez.net at gmail.com
Tue Aug 30 18:30:32 EDT 2005


Madhusudan Singh wrote:

> Madhusudan Singh wrote:
> 
>> Hi
>> 
>> I am using time.clock() to get the current time of the processor in
>> seconds. For my application, I need really high resolution but currently
>> seem to be limited to 0.01 second. Is there a way to specify the
>> resolution (say 1-10 microseconds) ? My processor is a 1.4 MHz Intel
>> processor. Surely, it should be able to report times a few (or at least
>> 10) microseconds apart.
>> 
>> Thanks.
> 
> Correcting a typo (1.4GHz, not 1.4 MHz).
> 
> And I am using Linux.

Then, this may be handy to give you an idea of the resolution you can expect at
the python level (i.e., without writing extension code).  Feel free to add
fancier statistics if you actually need them:

planck[python]> cat tdelta.py
#!/usr/bin/env python
"""quick and dirty test for time deltas.  Under Linux, this is best done
using time.time() instead of time.clock()"""

import commands
from time import time

npts = 50
times = [-(time()-time()) for i in xrange(npts)]

print commands.getoutput('egrep "MHz|model name" /proc/cpuinfo')
print 'Min. time delta :',min(times),'s'
print 'Max. time delta :',max(times),'s'
print 'Avg. time delta :',sum(times)/float(npts),'s'
print 'Num. of timings :',npts

#
For example, on my system:

planck[python]> ./tdelta.py
model name      : Intel(R) Pentium(R) 4 CPU 2.80GHz
cpu MHz         : 2794.365
Min. time delta : 2.86102294922e-06 s
Max. time delta : 9.05990600586e-06 s
Avg. time delta : 3.38554382324e-06 s
Num. of timings : 50

Cheers,

f




More information about the Python-list mailing list