RFC: startup time - again

Alexander Belchenko bialix at ukr.net
Fri Sep 12 09:43:21 BST 2008


Andrew Bennetts пишет:
> Alexander Belchenko wrote:
> [...]
>> PS: It seems like my timeit utitlity has the same bug as time.time() on Windows:
>> it has precision of 16 ms. I need to rewrite it to have more precise results.
>> I'll try to find implementation of time.clock() in Python sources. Anybody could
>> give me a hint?
> 
> <http://svn.python.org/view/python/trunk/Modules/timemodule.c?rev=64745&view=markup>
> 
> Look for “QueryPerformanceCounter” in that source, and you'll see the win32
> implementation of time.clock for Python.

Here is what I need:

/* 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);
}

Mark, I hope I can borrow this code, can I?




More information about the bazaar mailing list