Precise timings ?

M

Madhusudan Singh

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.
 
M

Michael Hudson

Madhusudan Singh said:
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) ?

Not in standard Python.
My processor is a 1.4 MHz Intel processor.

Mhz? :)
Surely, it should be able to report times a few (or at least 10)
microseconds apart.

It's probably architecture and operating system dependent. The
pentium has a timestamp counter that counts clock cycles and the
PowerPC has a similar (but slower) counter. You may need to write a
little C or asm.

Cheers,
mwh
 
B

Bengt Richter

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
^--G?
should be able to report times a few (or at least 10) microseconds apart.
windows or unix? time.time and time.clock seem to reverse roles as best-resolution
time sources depending on which platform.

If you have a pentium with a rdtsc instruction, you can write an extension module
in c to get at it. In C on a 300mhz p2 I got down to 23ns minimum delta between calls, IIRC.
But even then I was subject to the small probability of an interrupt during my timing loop,
and there are many normally going on, so the longer the interval you are timing the greater
the probability that an interrupt will occur within it (assuming random independence, which is
also not necessarily true). This is why timing frameworks generally either average brute force
or discard outlier timings and average, or return best-of. All these have different error
properties, and depend on lots of stuff.

I'm wondering whats your application is. Maybe you need a real-time OS?

Regards,
Bengt Richter
 
M

Madhusudan Singh

Madhusudan said:
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.
 
M

Madhusudan Singh

Bengt Richter wrote:

windows or unix? time.time and time.clock seem to reverse roles as
best-resolution time sources depending on which platform.

Linux.

time.time() seems to report (using %e to format the o/p) a fixed number of
the order ~1e9.
If you have a pentium with a rdtsc instruction, you can write an extension
module in c to get at it. In C on a 300mhz p2 I got down to 23ns minimum
delta between calls, IIRC. But even then I was subject to the small

I do not need ns resolution. A few microseconds would be fine.
probability of an interrupt during my timing loop, and there are many
normally going on, so the longer the interval you are timing the greater
the probability that an interrupt will occur within it (assuming random
independence, which is also not necessarily true). This is why timing
frameworks generally either average brute force or discard outlier timings
and average, or return best-of. All these have different error properties,
and depend on lots of stuff.

I'm wondering whats your application is. Maybe you need a real-time OS?

No, no. All I am looking for is for the system to report its time with
better resolution. I know it is possible on my hardware, because I can get
at very precise timings using Fortran 95 intrinsics in my other code.
However, with Python's time module, I do not seem to be able to access it.
 
F

Fredrik Lundh

Madhusudan said:
time.time() seems to report (using %e to format the o/p) a fixed number of
the order ~1e9.

you're confused. time.time() reports the wall time in fractional seconds
since the epoch (usually jan 1, 1970). if you take the difference between
two calls, you'll find that the resolution on a linux box is well over 100
kHz.

</F>
 
F

Fernando Perez

Madhusudan said:
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
 
M

Mike Meyer

Madhusudan Singh said:
No, no. All I am looking for is for the system to report its time with
better resolution. I know it is possible on my hardware, because I can get
at very precise timings using Fortran 95 intrinsics in my other code.

Then you could always write a C extensions module that calls an F95
routine to get that information. Your F95 compiler does interface with
C, doesn't it?

<mike
 
M

Madhusudan Singh

Fernando said:
Madhusudan said:
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

Thanks.
 

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

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,318
Members
47,998
Latest member
GretaCjy4

Latest Threads

Top