[Python-Dev] time.clock() on windows (original) (raw)

Mark Hammond mhammond at skippinet.com.au
Thu Oct 22 12:57:53 CEST 2009


On 22/10/2009 8:47 PM, Kristján Valur Jónsson wrote:

The point in question seems to be this this (from the thread): * Need some sort of static "start value", which is set when the process starts, so I can return to Python in seconds. An easy hack is to set this the first time clock() is called, but then it wont reflect any sort of real time - but would be useful for relative times...

But the argumentation is flawed.

It was made in the context of the APIs available to implement this. The code is short-and-sweet in timemodule.c, so please do go ahead and fix my flawed reasoning.

For reference:

#if defined(MS_WINDOWS) && !defined(BORLANDC) /* Due to Mark Hammond and Tim Peters */ static PyObject * time_clock(PyObject *self, PyObject *unused) { static LARGE_INTEGER ctrStart; static double divisor = 0.0; LARGE_INTEGER now; double diff;

if (divisor == 0.0) {
    LARGE_INTEGER freq;
    QueryPerformanceCounter(&ctrStart);
    if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
        /* Unlikely to happen - this works on all intel
           machines at least!  Revert to clock() */
        return PyFloat_FromDouble(((double)clock()) /
                      CLOCKS_PER_SEC);
    }
    divisor = (double)freq.QuadPart;
}
QueryPerformanceCounter(&now);
diff = (double)(now.QuadPart - ctrStart.QuadPart);
return PyFloat_FromDouble(diff / divisor);

}

Cheers,

Mark.



More information about the Python-Dev mailing list