time module precision

Tim Peters tim.peters at gmail.com
Sun Jan 9 01:11:13 EST 2005

[Jane Austine]
> What is the precision (and accuracy) of time module on a Windows XP
> machine?

There are many functions in the time module.  You shouldn't assume
that any two have similar behavior (because, in fact, they may not).

> threading module depends on time module so it's precision(and
> accuracy) is up to time module's.
> | for i in xrange(1000):
> |    time.sleep(0.001)
> This sleeps for awhile, which means 0.001 sec is not ignored. On the
> other hand,
> | for i in xrange(10000):
> |    time.sleep(0.0001)
> This returns right away.

It may or may not, depending on what other threads are trying to do.

> 0.0001 sec is ignored and lost in the air.
> So can I tell the time.sleep's precision is 1ms? Any other way to
> increase the precision? (Using busy waiting -- spinning loop -- is not
> a good option, it makes over 90% cpu usage)

Python's time.sleep() calls the Win32 API Sleep() function on Windows.
 All behavior is inherited from the latter.  See MS's docs:


In particular, MS Sleep() takes an integer argument, giving the number
of milliseconds to sleep.  Your 0.0001 case falls under the special
Sleep(0) case due to truncation in float->int conversion.

Also Google on

    sleep thread deviation

to find an interesting CodeProject article quantifying behavior on one
particular tester's Windows box.  Also see an article it references
for approaches to the question "how do I handle small intervals?"
(short course:  you probably can't, unless we're willing to throw
money at it).

More information about the Python-list mailing list