Closed
Description
The time.perf_counter()
function is implemented by calling QueryPerformanceCounter()
and QueryPerformanceFrequency()
. It computes QueryPerformanceCounter() * SEC_TO_SEC / QueryPerformanceFrequency()
using int64_t integers. The problem is that SEC_TO_SEC
is big: 10^9
.
QueryPerformanceFrequency() usually returns 10 MHz on Windows 10 and newer. The fraction SEC_TO_NS / frequency
= 1_000_000_000 / 10_000_000
can be simplified to 100 / 1
.
I propose using a fraction internally to convert QueryPerformanceCounter()
value to a number of seconds, and simplify the fraction using the Greatest Common Denominator (GCD).
There are multiple functions using a fraction:
_PyTime_GetClockWithInfo()
forclock()
--time.process_time()
in Python_PyTime_GetProcessTimeWithInfo()
fortimes()
--time.process_time()
in Pythonpy_get_monotonic_clock()
formach_absolute_time()
on macOS --time.monotonic()
in Pythonpy_get_win_perf_counter()
forQueryPerformanceCounter()
on Windows --time.perf_counter()
in Python
Linked PRs
Metadata
Metadata
Assignees
Labels
No labels