Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.4k
gh-88494: Use QueryPerformanceCounter() for time.monotonic()#116781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
On Windows, time.monotonic() now uses the QueryPerformanceCounter()clock to have a resolution better than 1 us, instead of thegGetTickCount64() clock which has a resolution of 15.6 ms.
vstinner commentedMar 14, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Script to measure the effective clock resolution: importtimedefcompute_resolution(func):resolution=Nonepoints=0timeout=time.monotonic()+1.0previous=func()whiletime.monotonic()<timeoutorpoints<3:forloopinrange(10):t1=func()t2=func()dt=t2-t1if0<dt:breakelse:dt=t2-previousifdt<=0.0:continueifresolutionisnotNone:resolution=min(resolution,dt)else:resolution=dtpoints+=1previous=func()returnresolutiondefformat_duration(dt):ifdt>=1e-3:return"%.0f ms"% (dt*1e3)ifdt>=1e-6:return"%.0f us"% (dt*1e6)else:return"%.0f ns"% (dt*1e9)deftest_clock(name,func):print("%s:"%name)resolution=compute_resolution(func)print("- efffective resolution: %s"%format_duration(resolution))clocks= ['time','perf_counter','monotonic']fornameinclocks:func=getattr(time,name)test_clock("%s()"%name,func)info=time.get_clock_info(name)print("- implementation: %s"%info.implementation)print("- resolution: %s"%format_duration(info.resolution))print() Results before (release build):
Results after (release build):
monotonic() resolution changes from 15.6 ms to 100 ns: it's156,000x more accurate than before! |
…ython#116781)On Windows, time.monotonic() now uses the QueryPerformanceCounter()clock to have a resolution better than 1 us, instead of thegGetTickCount64() clock which has a resolution of 15.6 ms.
…ython#116781)On Windows, time.monotonic() now uses the QueryPerformanceCounter()clock to have a resolution better than 1 us, instead of thegGetTickCount64() clock which has a resolution of 15.6 ms.
…ython#116781)On Windows, time.monotonic() now uses the QueryPerformanceCounter()clock to have a resolution better than 1 us, instead of thegGetTickCount64() clock which has a resolution of 15.6 ms.
Closesdask#8641._WindowsTime is no longer needed on Windows with Python 3.13. OnWindows, Python 3.13 now uses GetSystemTimePreciseAsFileTime() fortime.time() and QueryPerformanceCounter() for time.monotonic().*python/cpython#116781*python/cpython#116822
Closes#8641._WindowsTime is no longer needed on Windows with Python 3.13. OnWindows, Python 3.13 now uses GetSystemTimePreciseAsFileTime() fortime.time() and QueryPerformanceCounter() for time.monotonic().*python/cpython#116781*python/cpython#116822
MaxShvets commentedJan 23, 2025
@vstinner could you, please, help me figure out why the script works this way? Specifically, by taking 10 clock readings to measure |
With precise clocks such as QueryPerformanceCounter(), you don't go into the "else" block of the "for". The "else" is only there for low-resolution clocks. |
MaxShvets commentedJan 23, 2025
Got it, thanks |
Uh oh!
There was an error while loading.Please reload this page.
On Windows, time.monotonic() now uses the QueryPerformanceCounter() clock to have a resolution better than 1 us, instead of the gGetTickCount64() clock which has a resolution of 15.6 ms.
📚 Documentation preview 📚:https://cpython-previews--116781.org.readthedocs.build/