High-resolution timers

Tim Peters tim.one at home.com
Thu Jan 17 00:27:32 EST 2002

> I am searching for a platform-agnostic high resolution timer
> module to use in various entropy collection schemes for cryptographic
> purposes. The standard time module is great on my system ( i686 linux ) :
> >>> import time
> >>> time.time()
> 1011195116.002301
> It gives me microsecond precision, and for that I am grateful. But the
> module documentation warns:
> "Note that even though the time is always returned as a floating point
> number, not all systems provide time with a better precision than
> 1 second."
> I understand that the precision of the timer is hardware-dependent... But
> can someone give me an example of a platform on which time.time() has a
> resolution of less than 1 millisecond ?

Despite seductive pseudo-evidence presented to the contrary, on Windows
time.time() updates 18.2 times per second.  Windows rounds the value to a
multiple of 0.01, though, so it *appears* to be better than it is if you
don't look too closely.  This is a good approach, provided you know in
advance that time.time() sucks <wink>:

def hmm():
    from time import time, clock
    d = {}
    start = clock()
    for i in xrange(1000000):
        d[time()] = 1
    finish = clock()
    elapsed = finish - start
    print len(d), "unique /", elapsed, "seconds =", \
          len(d) / elapsed


Result from a typical run on Windows:

686 unique / 37.6919425401 seconds = 18.2001763181

Python's time.clock() (but not C's clock()!) on Windows updates 1,193,180
times per second on most boxes, more often on some, and "beats me" under
64-bit Windows.  Unlike as on Unix, time.clock() deltas measure wall-clock
time on Windows, not user time (ya, ya, it's really derived indirectly from
the Pentium's cycle counter, but "wall-clock time" captures the high-order

IIRC, Mac Classic doesn't have any timer that updates more than 60 times per

Etc.  Using time.time() on Linux and time.clock() on Windows should be good
enough, unless you care about other platforms too.  Do yourself a favor and
don't <wink/sigh>.

More information about the Python-list mailing list