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-88494: Use QueryPerformanceCounter() for time.monotonic() by vstinner · Pull Request #116781 · 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

On Windows, time.monotonic() now uses the QueryPerformanceCounter() clock to have a resolution better than 1 us, instead of the GetTickCount64() clock which has a resolution of 15.6 ms.

  • Issue: time.monotonic() should use a different clock source on Windows #88494

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

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 08:33
@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
gh-115637 clarify that time.perf_counter cannot go backwards #115638
Closed
@vstinner
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic()
28d2357
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.
@vstinner
vstinner force-pushed the QPC branch from 84632e9 to 28d2357 Compare March 14, 2024 08:37
@vstinner

vstinner commented Mar 14, 2024

Copy link
Copy Markdown
Member Author

See also #115638 and issue gh-115637.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner
vstinner merged commit 846ad5a into python:main Mar 14, 2024
@vstinner
vstinner deleted the QPC branch March 14, 2024 15:42
@bedevere-app bedevere-app Bot removed the awaiting core review label Mar 14, 2024
@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 Author

Script to measure the effective clock resolution:

import time

def compute_resolution(func):
    resolution = None
    points = 0
    timeout = time.monotonic() + 1.0
    previous = func()
    while time.monotonic() < timeout or points < 3:
        for loop in range(10):
            t1 = func()
            t2 = func()
            dt = t2 - t1
            if 0 < dt:
                break
        else:
            dt = t2 - previous
            if dt <= 0.0:
                continue
        if resolution is not None:
            resolution = min(resolution, dt)
        else:
            resolution = dt
        points += 1
        previous = func()
    return resolution

def format_duration(dt):
    if dt >= 1e-3:
        return "%.0f ms" % (dt * 1e3)
    if dt >= 1e-6:
        return "%.0f us" % (dt * 1e6)
    else:
        return "%.0f ns" % (dt * 1e9)

def test_clock(name, func):
    print("%s:" % name)
    resolution = compute_resolution(func)
    print("- efffective resolution: %s" % format_duration(resolution))


clocks = ['time', 'perf_counter', 'monotonic']
for name in clocks:
    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 ms

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

monotonic():
- 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 ms

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

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

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Mar 15, 2024
gh-63207: Use GetSystemTimePreciseAsFileTime() in time.time() #116822
Merged
vstinner added a commit to vstinner/cpython that referenced this pull request Mar 20, 2024
@vstinner
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic() (p…
ae432c8
…ython#116781)

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.
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
@vstinner @adorilson
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic() (p…
64bd681
…ython#116781)

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.
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@vstinner @diegorusso
pythongh-88494: Use QueryPerformanceCounter() for time.monotonic() (p…
1169a0a
…ython#116781)

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.
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
@morotti morotti mentioned this pull request Jul 11, 2024
Use higher precision time source in retry decorator/tests and avoid flaky failures pypa/pip#12839
Merged
@MaxShvets

MaxShvets commented Jan 23, 2025

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Jan 23, 2025

Copy link
Copy Markdown
Member Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

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

@MaxShvets

MaxShvets commented Jan 23, 2025

Copy link
Copy Markdown

Got it, thanks

All reactions

Sorry, something went wrong.

Uh oh!

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

@longcw longcw mentioned this pull request Jul 1, 2025
fix schedule speech on windows when monotonic_ns resolution is rough livekit/agents#2770
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@pganssle pganssle Awaiting requested review from pganssle pganssle is a code owner
@abalkin abalkin Awaiting requested review from abalkin

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.

2 participants

@vstinner @MaxShvets
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.