Useing the processor clock, or get time in Femptoseconds

John Nagle nagle at animats.com
Mon Jan 31 00:13:14 EST 2011


On 1/30/2011 8:14 PM, Littlefield, Tyler wrote:
> If you are on windows, you can use high-resolution timers. What you are
> trying is physically impossible though: lets say you have a processor
> that runs at 2.5 GHz. that's 2.5 billion cycles per second, give or take
> a few. So, the lowest you can go is nanoseconds. You're trying to time
> like 10x the processor's actual speed, and you're not going to get
> timing that good. so, lower your bar a bit; the highest you will get is
> nanoseconds with high-res timers. (I'm not sure what the equivalent of
> this is on *nix, or whether or not python supports it on either
> platform. I think you'll end up making a DLL call, though I could be
> wrong).

    On x86 CPUs since the Pentium, you can read the CPU's cycle counter,
which is incrementing once for each clock cycle, maybe every 300ps
or so on a current generation CPU.
See "http://en.wikipedia.org/wiki/Time_Stamp_Counter"

    You do not want to do this.  First, you're in Python, which is
interpreted; picosecond level timing resolution is orders of magnitude
finer than is meaningful at the Python source level. Second,
on multiprocessors, each CPU has its own cycle clock, and they're not
synchronized.  Third, on superscalar CPUs, reading of the clock may
occur out of order and be meaningless.

    In Python, just call "time.time()", which gets you a floating point
number with about 1ms resolution.

					John Nagle





More information about the Python-list mailing list