[Python-Dev] Use QueryPerformanceCounter() for time.monotonic() and/or time.highres()?

Kristján Valur Jónsson kristjan at ccpgames.com
Tue Apr 3 16:22:13 CEST 2012

> -----Original Message-----
> From: python-dev-bounces+kristjan=ccpgames.com at python.org
> Some years? I computed 584.5 years, so it should not occur in practice.

Funny that you mention it. "should not occur in practice" is exactly my point.

> > Here's actual code from production:
> >
> > BOOL WINAPI QueryPerformanceCounterCCP( LARGE_INTEGER* li ) {
> >        static LARGE_INTEGER last = {0};
> >        BOOL ok = QueryPerformanceCounter(li);
> >        if( !ok )
> >        {
> >                return FALSE;
> >        }
> Did you already see it failing in practice? Python ignores the return value and
> only uses the counter value.
No, actually not.  But we always check return codes.  Always.

> > Even negative delta values of time are usually  harmless on the application
> level.
> > A curiosity, but harmless.
> It depends on your usecase. For a scheduler or to implement a timeout, it
> does matter. 
Does it?
now = time.wallclock()
if job.due_time <= now:

So what if you get an early timeout?  timeouts aren't guaranteed to wait _at least_ the specified time.  Rather to wait _at most_ the specified time.  

> The PEP contains also different proofs that QPC is not steady, especially on
> virtual machines.
What does "steady" mean? 
Sampled time on a computer will always differ from some ideal time measured by an atomic clock.  On a virtual machine, the probability distribution of the error function can be different than on a "real" machine, but saying it is different is not enough.  You have to quantify it somehow.  And unless there is some "accepted" error PDF, there is no way to say that some platforms are ok, and others not.

And what does it matter?  A virtual machine is just another platform, one where we are providing a counter as good as that platform can provide.  "Caveat emptor: Don't expect reliable benchmarks or smoothly running time on a virtual machine.  The wallclock() function will contain some undetermined error depending on the quality of your platform."

I think you are simply overcomplicating the problem and trying to promise too much, without even being able to properly quantify that promise.  Just relax the specification and all will be well.


More information about the Python-Dev mailing list