Edit: On Windows 10, this nonsense seems unnecessary. Try it like this:
>>> from time import sleep >>> import timeit >>> '%.2f%% overhead' % (timeit.timeit('sleep(0.025)', number=100, globals=globals()) / 0.025 - 100) '0.29% overhead'
.29% or so, rather low overhead and usually more than fairly accurate.
Previous versions of Windows will by default have a sleep resolution of 55 ms, which means that your sleep call will take from 25 to 55 ms. To get the sleep resolution to 1 millisecond, you need to set the resolution used by Windows by calling timeBeginPeriod :
import ctypes winmm = ctypes.WinDLL('winmm') winmm.timeBeginPeriod(1)
source share