Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-63207: Use GetSystemTimePreciseAsFileTime() in time.time() #116822

Merged
merged 1 commit into from
Mar 18, 2024

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Mar 14, 2024

@vstinner vstinner changed the title gh-88494: Use GetSystemTimePreciseAsFileTime() in time.time() gh-63207: Use GetSystemTimePreciseAsFileTime() in time.time() Mar 14, 2024
@vstinner
Copy link
Member Author

cc @pitrou @eryksun @zooba

@vstinner
Copy link
Member Author

Clock resolution measured by the script: #116781 (comment)

Before:

time():
- efffective resolution: 15 ms
- implementation: GetSystemTimeAsFileTime()
- resolution: 16 ms

perf_counter():
- efffective resolution: 100 ns
- implementation: QueryPerformanceCounter()
- resolution: 100 ns

monotonic():
- efffective resolution: 100 ns
- implementation: QueryPerformanceCounter()
- resolution: 100 ns

After:

time():
- efffective resolution: 238 ns
- implementation: GetSystemTimePreciseAsFileTime()
- resolution: 100 ns

perf_counter():
- efffective resolution: 100 ns
- implementation: QueryPerformanceCounter()
- resolution: 100 ns

monotonic():
- efffective resolution: 100 ns
- implementation: QueryPerformanceCounter()
- resolution: 100 ns

time.time() resolution is way better! 238 ns instead of 15.6 ms, it's around 65 500x more accurate!

@vstinner
Copy link
Member Author

On a release build (build using PCbuild\build.bat -e -p x64 command), I confirm that GetSystemTimePreciseAsFileTime() ("change") is slower than GetSystemTimeAsFileTime() ("ref"):

vstinner@WIN C:\victor\python\main>env\Scripts\python -m pyperf compare_to ref.json change.json                                           
Mean +- std dev: [ref] 35.8 ns +- 0.8 ns -> [change] 52.9 ns +- 2.2 ns: 1.48x slower

Well, more and more operating have two clocks: one slow but accurate, and one fast but less accurate. Example on Linux:

  • CLOCK_REALTIME
  • CLOCK_REALTIME_COARSE: A faster but less precise version of CLOCK_REALTIME

Python uses CLOCK_REALTIME for time.time(), the most precise one. So it's consistent to use the most accurate clock for time.time() on Windows as well.

If tomorrow, performance becomes an issue, we can add a new time.time_coarse() function which would use CLOCK_REALTIME_COARSE on Linux, and GetSystemTimeAsFileTime() on Windows.

@pitrou
Copy link
Member

pitrou commented Mar 15, 2024

time.time() resolution is way better! 238 ns instead of 15.6 ms, it's around 65 500x more accurate!

Impressive! Finally Windows will be as good as other platforms here.

@@ -55,6 +55,14 @@
#endif


#ifdef MS_WINDOWS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think checking for MS_WINDOWS here is sufficient. GetSystemTimePreciseAsFileTime(), QueryPerformanceCounter(), and QueryPerformanceFrequency() are available in the system, app, and game partitions. The old use of GetSystemTimeAdjustment() actually wasn't supported for app builds. However, that function was used improperly anyway.

@vstinner vstinner merged commit 1d95451 into python:main Mar 18, 2024
33 of 34 checks passed
@vstinner vstinner deleted the time_precise branch March 18, 2024 16:13
vstinner added a commit to vstinner/distributed that referenced this pull request May 7, 2024
Closes dask#8641.

_WindowsTime is no longer needed on Windows with Python 3.13. On
Windows, Python 3.13 now uses GetSystemTimePreciseAsFileTime() for
time.time() and QueryPerformanceCounter() for time.monotonic().

* python/cpython#116781
* python/cpython#116822
fjetter pushed a commit to dask/distributed that referenced this pull request May 8, 2024
Closes #8641.

_WindowsTime is no longer needed on Windows with Python 3.13. On
Windows, Python 3.13 now uses GetSystemTimePreciseAsFileTime() for
time.time() and QueryPerformanceCounter() for time.monotonic().

* python/cpython#116781
* python/cpython#116822
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants