How Accurate Is Python's Time.Sleep()

How accurate is python's time.sleep()?

The accuracy of the time.sleep function depends on your underlying OS's sleep accuracy. For non-realtime OS's like a stock Windows the smallest interval you can sleep for is about 10-13ms. I have seen accurate sleeps within several milliseconds of that time when above the minimum 10-13ms.

Update:
Like mentioned in the docs cited below, it's common to do the sleep in a loop that will make sure to go back to sleep if it wakes you up early.

I should also mention that if you are running Ubuntu you can try out a pseudo real-time kernel (with the RT_PREEMPT patch set) by installing the rt kernel package (at least in Ubuntu 10.04 LTS).

EDIT: Correction non-realtime Linux kernels have minimum sleep interval much closer to 1ms then 10ms but it varies in a non-deterministic manner.

How accurate is python's time.sleep() for long time periods?

Python's time.sleep() function is accurate and should be used in this case as it is simpler and easier to run. An example is

time.sleep(10000) # will stop all running scripts in the same pid

Using a bare while statement without any threshold will use a lot of your resources, which is why you should use a time.sleep expression to reduce this. You also should have used the while statement condition statement as this will make sure your while statement closes.

As shown below

end = time.time() + 10000

while end > time.time(): # ensures to end when time.time (now) is more than end
time.sleep(0.001) # creates a 1 ms gap to decrease cpu usage

How to make a delay at ~100-200us

About accuracy of time.sleep

It looks like time.sleep() only has millisecond resolution. Though it could range between a few milliseconds to over 20 milliseconds (best case scenarios), based on what OS you are using, it does not look like you will get accurate microsecond sleep execution from it.

References

  1. Must See This: How accurate is python's time.sleep()?
  2. usleep in Python
  3. https://github.com/JuliaLang/julia/issues/12770
  4. https://gist.github.com/ufechner7/1aa3e96a8a5972864cec

Another Option:

  • Take a look at the high precision timers available from winmm.lib: Creating a High-Precision, High-Resolution, and Highly Reliable Timer, Utilising Minimal CPU Resources. This resource shares some code (C++ perhaps) to create a high precision timer (they claim a 5% error) and if the precision happens to be at the order of 1 microsecond, then you probably be okay with it.
  • Also see this: How to make thread sleep less than a millisecond on Windows.

You could potentially then look for creating a C/C++ wrapper around the timer-code to use it from Python.

Why is time.sleep() accuracy influenced by Chrome?

I extra fired up Windows 7 to replicate your findings and I can confirm it.

It's a Windows thing with the type of timer used and a default resolution of 15.6 ms (minimum 0.5 ms). Applications can alter the current resolution (WinAPI function: timeBeginPeriod) and Chrome does so.

This function affects a global Windows setting. Windows uses the
lowest value (that is, highest resolution) requested by any process.
Setting a higher resolution can improve the accuracy of time-out
intervals in wait functions. However, it can also reduce overall
system performance, because the thread scheduler switches tasks more
often. High resolutions can also prevent the CPU power management
system from entering power-saving modes. Setting a higher resolution
does not improve the accuracy of the high-resolution performance
counter.

An article from 2014 in Forbes is covering a bug in Chrome which would set the resolution permanently to 1 ms no matter what current load would require - a problem because it's a system-wide effect with impact on energy consumption. From that article:

In an OS like Windows, events are often set to run at intervals. To
save power, the processor sleeps when nothing needs attention, and
wakes at predefined intervals. This interval is what Chrome adjusts in
Windows, so reducing it to 1.000ms means that the system is waking far
more often than at 15.625ms. In fact, at 1.000ms the processor is
waking 1000 times per second. The default, of 15.625ms means the
processor wakes just 64 times per second to check on events that need
attention.

Microsoft itself says that tick rates of 1.000ms might increase power
consumption by "as much as 25 per cent".

You can get the default resolution from Python with time.get_clock_info().

namespace = time.get_clock_info('time')
namespace.adjustable
# True
namespace.implementation
# 'GetSystemTimeAsFileTime()'
namespace.monotonic
# False
namespace.resolution
# 0.015600099999999999

You can get the actual resolution from cmd with the ClockRes applet.

Python time.sleep vs busy wait accuracy

On Windows the OS Sleep function (which Python necessarily uses) can only wake up a thread on a multiple of the current timer interval. Typically this ranges between 1.0 ms and 15.6 ms. Lowering the timer interval can be handy because it allows for shorter sleeps, but it wastes electricity, as I wrote about in this article:

http://randomascii.wordpress.com/2013/07/08/windows-timer-resolution-megawatts-wasted/

Busy waiting may give better accuracy but is generally a horrible idea since it wastes even more electricity and steals CPU time from more deserving tasks:

https://randomascii.wordpress.com/2012/06/05/in-praise-of-idleness/

Finally, the accuracy of busy waiting will depend on what timer function you are using to get the current time, and may also depend on the timer interval:

https://randomascii.wordpress.com/2013/05/09/timegettime-versus-gettickcount/

Why do you want to sleep for such short time periods? Usually it would be better to wait for something to happen -- waiting on an event -- rather than waiting for such short time periods.



Related Topics



Leave a reply



Submit