GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-63207: Use GetSystemTimePreciseAsFileTime() in time.time() by vstinner · Pull Request #116822 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Mar 14, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

📚 Documentation preview 📚: https://cpython-previews--116822.org.readthedocs.build/

  • Issue: precise time.time() under Windows 8: use GetSystemTimePreciseAsFileTime #63207

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
vstinner requested review from abalkin and pganssle as code owners March 14, 2024 16:39
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 14, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Mar 14, 2024
time.monotonic() should use a different clock source on Windows #88494
Closed
@vstinner vstinner mentioned this pull request Mar 14, 2024
precise time.time() under Windows 8: use GetSystemTimePreciseAsFileTime #63207
Closed
@vstinner
vstinner force-pushed the time_precise branch from 4ef53f4 to 85790f4 Compare March 14, 2024 16:42
@vstinner vstinner changed the title gh-88494: Use GetSystemTimePreciseAsFileTime() in time.time() gh-63207: Use GetSystemTimePreciseAsFileTime() in time.time() Mar 14, 2024
@vstinner

vstinner commented Mar 14, 2024

Copy link
Copy Markdown
Member Author

cc @pitrou @eryksun @zooba

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
pythongh-63207: Use GetSystemTimePreciseAsFileTime() in time.time()
2ea920e
@vstinner
vstinner force-pushed the time_precise branch from 3b6c7b3 to 2ea920e Compare March 14, 2024 19:10
@vstinner

vstinner commented Mar 15, 2024

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Mar 15, 2024

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pitrou

pitrou commented Mar 15, 2024

Copy link
Copy Markdown
Member

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.

vstinner and eryksun reacted with hooray emoji
All reactions
  • 🎉 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

eryksun
eryksun reviewed Mar 16, 2024
View reviewed changes
Comment thread Python/pytime.c
#endif


#ifdef MS_WINDOWS

@eryksun eryksun Mar 16, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
eryksun
eryksun approved these changes Mar 16, 2024
View reviewed changes
@vstinner
vstinner removed request for abalkin and pganssle March 17, 2024 13:33
@vstinner
vstinner merged commit 1d95451 into python:main Mar 18, 2024
@vstinner
vstinner deleted the time_precise branch March 18, 2024 16:13
@bedevere-app bedevere-app Bot removed the awaiting core review label Mar 18, 2024
vstinner added a commit to vstinner/cpython that referenced this pull request Mar 20, 2024
@vstinner
pythongh-63207: Use GetSystemTimePreciseAsFileTime() in time.time() (p…
ec52c0d
…ython#116822)
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
@vstinner @adorilson
pythongh-63207: Use GetSystemTimePreciseAsFileTime() in time.time() (p…
b50f8f3
…ython#116822)
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@vstinner @diegorusso
pythongh-63207: Use GetSystemTimePreciseAsFileTime() in time.time() (p…
7d4e89c
…ython#116822)
vstinner added a commit to vstinner/distributed that referenced this pull request May 7, 2024
@vstinner
Fix clocks on Windows with Python 3.13
f10c398
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
@vstinner vstinner mentioned this pull request May 7, 2024
Fix clocks on Windows with Python 3.13 dask/distributed#8642
Merged
1 of 2 tasks
fjetter pushed a commit to dask/distributed that referenced this pull request May 8, 2024
@vstinner
Fix clocks on Windows with Python 3.13 (#8642)
e4a0545
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
@vstinner vstinner mentioned this pull request Oct 3, 2024
gh-124832: Add a note to indicate that datetime.now may return the same instant #124834
Merged
@xrmx xrmx mentioned this pull request Jan 17, 2025
Implement higher precision clock than time_ns open-telemetry/opentelemetry-python#4367
Open
vstinner pushed a commit that referenced this pull request Jun 30, 2025
@StanFromIreland
gh-63207: Update time.time documentation after #116822 (#136068)
486587d
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 30, 2025
@StanFromIreland @miss-islington
pythongh-63207: Update time.time documentation after pythonGH-116822 (
8fa0f6a
pythonGH-136068)

(cherry picked from commit 486587d)

Co-authored-by: Stan Ulbrych <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 30, 2025
@StanFromIreland @miss-islington
pythongh-63207: Update time.time documentation after pythonGH-116822 (
31cf81f
pythonGH-136068)

(cherry picked from commit 486587d)

Co-authored-by: Stan Ulbrych <[email protected]>
vstinner pushed a commit that referenced this pull request Jun 30, 2025
@miss-islington @StanFromIreland
[3.14] gh-63207: Update time.time documentation after GH-116822 (GH…
401032a
…-136068) (#136138)

gh-63207: Update `time.time` documentation after GH-116822 (GH-136068)
(cherry picked from commit 486587d)

Co-authored-by: Stan Ulbrych <[email protected]>
vstinner pushed a commit that referenced this pull request Jun 30, 2025
@miss-islington @StanFromIreland
[3.13] gh-63207: Update time.time documentation after GH-116822 (GH…
b415d1c
…-136068) (#136139)

gh-63207: Update `time.time` documentation after GH-116822 (GH-136068)
(cherry picked from commit 486587d)

Co-authored-by: Stan Ulbrych <[email protected]>
AndPuQing pushed a commit to AndPuQing/cpython that referenced this pull request Jul 11, 2025
@StanFromIreland @AndPuQing
pythongh-63207: Update time.time documentation after python#116822 (p…
1dc67db
…ython#136068)
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@StanFromIreland @Pranjal095
pythongh-63207: Update time.time documentation after python#116822 (p…
b57301c
…ython#136068)
picnixz pushed a commit to picnixz/cpython that referenced this pull request Jul 13, 2025
@StanFromIreland @picnixz
pythongh-63207: Update time.time documentation after python#116822 (p…
5a22861
…ython#136068)
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@StanFromIreland @taegyunkim
pythongh-63207: Update time.time documentation after python#116822 (p…
f72b465
…ython#136068)
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
@StanFromIreland @Agent-Hellboy
pythongh-63207: Update time.time documentation after python#116822 (p…
6032001
…ython#136068)
@krassowski krassowski mentioned this pull request Aug 26, 2025
Improve performance by storing and applying checkpoints to reduce loading time y-crdt/pycrdt-store#7
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eryksun eryksun eryksun approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@vstinner @pitrou @eryksun
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.