Skip to content

Reported resolution of time.process_time and time.thread_time is wrong on Windows #135304

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

Open
chris-eibl opened this issue Jun 9, 2025 · 2 comments
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@chris-eibl
Copy link
Member

chris-eibl commented Jun 9, 2025

Bug report

Bug description:

>>> import time
>>> time.get_clock_info("process_time")
namespace(implementation='GetProcessTimes()', monotonic=True, adjustable=False, resolution=1e-07)
>>> time.get_clock_info("thread_time")
namespace(implementation='GetThreadTimes()', monotonic=True, adjustable=False, resolution=1e-07)

The reported resolution=1e-07 is way off. According to @eryksun in #82040 (comment)

Don't read too much into the clock info here:
Process times [1] are stored as a 64-bit integer in units of 100 ns (1e-7). But the kernel schedules threads based on a timer that ticks every 15.625 ms by default. It can be lowered to about 0.5 ms, but this degrades battery life.

This has also caused problems in test_int.test_denial_of_service, see #114911 (comment).

I suggest to report 15.625 ms, since this is the upper limit.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

@chris-eibl chris-eibl added type-bug An unexpected behavior, bug, or error OS-windows stdlib Python modules in the Lib dir labels Jun 9, 2025
@chris-eibl chris-eibl changed the title Reported resolution of time.process_time and time.thread.time is wrong on Windows Reported resolution of time.process_time and time.thread_time is wrong on Windows Jun 9, 2025
@vstinner
Copy link
Member

But the kernel schedules threads based on a timer that ticks every 15.625 ms by default. It can be lowered to about 0.5 ms, but this degrades battery life.

Is there an API to query the clock resolution?

@zooba
Copy link
Member

zooba commented Jun 10, 2025

Only this undocumented one, which seems pretty unlikely to change, given the amount of "optimisation" software that uses it already, but I don't think we want to put Python into that category.

I'm yet to see a problem critical enough to require needing to know an exact value.

FWIW, the concept of increasing the timer resolution is meant for temporary, per-process use. It's not meant to be a system-wide setting, it just happens to have the effect that the system timer has to match the highest resolution request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants