By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

Precise timings ?

P: n/a
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.
Aug 30 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Madhusudan Singh <sp**************@spam.invalid> writes:
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

--
All of us in here are guilty as hell. Justified, yes, but guilty.
"and that, your honour, was when I killed him"
"Well, I see. That's all right then"
-- Devin Rubia, asr
Aug 30 '05 #2

P: n/a
On Tue, 30 Aug 2005 13:36:57 -0400, Madhusudan Singh <sp**************@spam.invalid> 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 ^--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
Aug 30 '05 #3

P: n/a
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.
Aug 30 '05 #4

P: n/a
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.
Aug 30 '05 #5

P: n/a
Madhusudan Singh wrote:
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>

Aug 30 '05 #6

P: n/a
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

Aug 30 '05 #7

P: n/a
Madhusudan Singh <sp**************@spam.invalid> writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Aug 31 '05 #8

P: n/a
Fernando Perez wrote:
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


Thanks.
Aug 31 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.