Eryk Sun <eryk...@gmail.com> added the comment:

You resolved bpo-41299 using QueryPerformanceCounter(), so we're already a step 
toward making it the default monotonic clock. Personally, I've only relied on 
QPC for short intervals, but, as you've highlighted above, other language 
runtimes use it for their monotonic clock. Since Vista, it's apparently more 
reliable in terms of calibration and ensuring that a processor TSC is only used 
if it's known to be invariant and constant.

That said, Windows 10 also provides QueryInterruptTimePrecise(), which is a 
hybrid solution. It uses the performance counter to interpolate a timestamp 
between interrupts. I'd prefer to use this for time.monotonic() instead of QPC, 
if it's available via GetProcAddress().

QueryInterruptTimePrecise() is about 1.38 times the cost of QPC (on average 
across 100 million calls). Both functions are significantly more expensive than 
QueryInterruptTime() and GetTickCount64(), which simply return a value that's 
read from shared memory (i.e. the KUSER_SHARED_DATA structure).

> QueryUnbiasedInterruptTime() is available on Windows 8 while 
> QueryInterruptTime() is new as of Windows 10. The "Unbiased" 
> just refers to whether it advances during sleep.

QueryInterruptTime() and QueryUnbiasedInterruptTime() don't provide 
high-resolution timestamps. They're updated by the system timer interrupt 
service routine, which defaults to 64 interrupts/second. The time increment 
depends on when the counter is read by the ISR, but it averages out to 
approximately the interrupt period (e.g. 15.625 ms).

> I'm not actually sure whether time.monotonic() in Python counts 
> time spent asleep, or whether that's desirable. 

POSIX doesn't specify whether CLOCK_MONOTONIC [1] should include the time that 
elapses while the system is in standby mode. In Linux, CLOCK_BOOTTIME includes 
this time, and CLOCK_MONOTONIC excludes it. Windows 
QueryUnbiasedInterruptTime[Precise]() excludes it.

> Perhaps the long term answer would be to introduce separate 
> "asleep" and "awake" monotonic clocks in Python

Both may not be supportable on all platforms, but they're supported in Linux, 
Windows 10, and macOS. The latter has mach_continuous_time(), which includes 
the time in standby mode, and mach_absolute_time(), which excludes it.

--- 
[1] 
https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html

----------
nosy: +eryksun

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44328>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to