You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
time.monotonic() should use a different clock source on Windows #88494
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-06-06.22:05:07.766>labels= ['3.11', 'library', 'OS-windows', 'performance']
title='time.monotonic() should use a different clock source on Windows'updated_at=<Date2021-06-14.21:10:46.833>user='https://github.com/lunixbochs'
On systems that run Windows XP or later, the function will always succeed and will thus never return zero
I've run into issues with this when porting python-based applications to Windows. On other platforms, time.monotonic() was a decent precision so I used it. When I ported to Windows, I had to replace all of my time.monotonic() calls with time.perf_counter(). I would pretty much never knowingly call time.monotonic() if I knew ahead of time it could be quantized to 16ms.
My opinion is that the GetTickCount64() monotonic time code in CPython should be removed entirely and only the QueryPerformanceCounter() path should be used.
I also think some of the failure checks could be removed from QueryPerformanceCounter() / QueryPerformanceFrequency(), as they're documented to never fail in modern Windows and CPython has been dropping support for older versions of Windows, but that's less of a firm opinion.
time.monotonic() now uses QueryPerformanceCounter() (commit) and has a resolution lower than 1 us. I measured 100 ns (10 MHz) on my Windows 11 VM, whereas before it was 15.6 ms (64 Hz): the updated clock resolution is 156,000x better :-)
Thanks everybody for looking into this issue and providing very useful technical details about these clocks.
Follow-up: see issue gh-63207 to use GetSystemTimePreciseAsFileTime() for time.time().
Activity
lunixbochs commentedon Jun 6, 2021
Related to https://bugs.python.org/issue41299#msg395220
Presumably
time.monotonic()on Windows historically used GetTickCount64() because QueryPerformanceCounter() could fail. However, that hasn't been the case since Windows XP: https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounterI've run into issues with this when porting python-based applications to Windows. On other platforms, time.monotonic() was a decent precision so I used it. When I ported to Windows, I had to replace all of my time.monotonic() calls with time.perf_counter(). I would pretty much never knowingly call time.monotonic() if I knew ahead of time it could be quantized to 16ms.
My opinion is that the GetTickCount64() monotonic time code in CPython should be removed entirely and only the QueryPerformanceCounter() path should be used.
I also think some of the failure checks could be removed from QueryPerformanceCounter() / QueryPerformanceFrequency(), as they're documented to never fail in modern Windows and CPython has been dropping support for older versions of Windows, but that's less of a firm opinion.
28 remaining items
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic()
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic()
vstinner commentedon Mar 14, 2024
I created #116781 to use QueryPerformanceCounter() for time.monotonic().
vstinner commentedon Mar 14, 2024
See also issue gh-115637.
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic()
gh-88494: Use QueryPerformanceCounter() for time.monotonic() (#116781)
pythongh-88494: Use GetSystemTimePreciseAsFileTime() in time.time()
pythongh-88494: Use GetSystemTimePreciseAsFileTime() in time.time()
vstinner commentedon Mar 15, 2024
time.monotonic() now uses QueryPerformanceCounter() (commit) and has a resolution lower than 1 us. I measured 100 ns (10 MHz) on my Windows 11 VM, whereas before it was 15.6 ms (64 Hz): the updated clock resolution is 156,000x better :-)
Thanks everybody for looking into this issue and providing very useful technical details about these clocks.
Follow-up: see issue gh-63207 to use GetSystemTimePreciseAsFileTime() for time.time().
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic() (p…
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic() (p…
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic() (p…
pythongh-88494: Reduce CLOCK_RES to 1 ms in tests