Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
vstinner merged 1 commit intopython:mainfromvstinner:QPC
Mar 14, 2024

Conversation

vstinner
Copy link
Member

@vstinnervstinner commentedMar 14, 2024
edited by github-actionsbot
Loading

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/

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
Copy link
MemberAuthor

See also#115638 and issuegh-115637.

@vstinnervstinner merged commit846ad5a intopython:mainMar 14, 2024
@vstinnervstinner deleted the QPC branchMarch 14, 2024 15:42
@vstinner
Copy link
MemberAuthor

vstinner commentedMar 14, 2024
edited
Loading

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):

time():- efffective resolution: 2 ms- implementation: GetSystemTimeAsFileTime()- resolution: 16 msperf_counter():- efffective resolution: 100 ns- implementation: QueryPerformanceCounter()- resolution: 100 nsmonotonic():- efffective resolution: 15 ms- implementation: GetTickCount64()- resolution: 16 ms

Results after (release build):

Running Release|x64 interpreter...time():- efffective resolution: 1 ms- implementation: GetSystemTimeAsFileTime()- resolution: 16 msperf_counter():- efffective resolution: 100 ns- implementation: QueryPerformanceCounter()- resolution: 100 nsmonotonic():- efffective resolution: 100 ns- implementation: QueryPerformanceCounter()- resolution: 100 ns

monotonic() resolution changes from 15.6 ms to 100 ns: it's156,000x more accurate than before!

vstinner added a commit to vstinner/cpython that referenced this pull requestMar 20, 2024
…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.
adorilson pushed a commit to adorilson/cpython that referenced this pull requestMar 25, 2024
…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.
diegorusso pushed a commit to diegorusso/cpython that referenced this pull requestApr 17, 2024
…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.
vstinner added a commit to vstinner/distributed that referenced this pull requestMay 7, 2024
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
fjetter pushed a commit to dask/distributed that referenced this pull requestMay 8, 2024
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
Copy link

@vstinner could you, please, help me figure out why the script works this way? Specifically, by taking 10 clock readings to measuredt, don't we end up measuring resolution times 10?

@vstinner
Copy link
MemberAuthor

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
Copy link

Got it, thanks

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@pgansslepganssleAwaiting requested review from pgansslepganssle is a code owner

@abalkinabalkinAwaiting requested review from abalkinabalkin is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@vstinner@MaxShvets

[8]ページ先頭

©2009-2025 Movatter.jp