[Python-checkins] r46505 -python/trunk/Tools/pybench/systimes.py

M.-A. Lemburg mal at egenix.com
Tue Jun 6 16:07:18 CEST 2006


Kristján V. Jónsson wrote:
> FYI, in EVE, we have a so-called tasklet-timer module which times the execution of selected contexts.  We use both QueryPerformanceCounter() for wallclock time, and GetThreadTimes() for user and kernel time.  We sum the user and kernel times to get total execution time.  We compare that to  the wallclock time to get thread cpu utilization.  It works very well.  Oh, and we use GetProcessTimes() to compare the load of the main python thread with the process totality, including DB and other IO threads.

Thanks for the info. From the docs, it seems that GetThreadTimes()
is the same as GetProcessTimes(), only on a per-thread basis:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getthreadtimes.asp

pybench will only run a single thread, so GetProcessTimes() should
already have the information we need.

> I am not sure how often the process times are updated.  It may happen more often than on the clock interrupt, the scheduler might actually tally times spent.  Seems flaky to sample the PC.  However, experiments are the easiest way to answer that.

This API allows (indirectly) querying the clock tick rate:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsystemtimeadjustment.asp

(this is what the tools uses that Fredrik pointed to)

Here's some code to access that API in Python:

import ctypes

def GetSystemTimeAdjustment():
    timeadjustment = ctypes.c_long()
    timeincrement = ctypes.c_long()
    adjdisabled = ctypes.c_long()
    rc = ctypes.windll.kernel32.GetSystemTimeAdjustment(
        ctypes.byref(timeadjustment),
        ctypes.byref(timeincrement),
        ctypes.byref(adjdisabled))
    if not rc:
        raise TypeError('GetSystemTimeAdjustment() returned an error')
    return (timeadjustment.value * 10e-9, timeincrement.value * 10e-9)

print ('The clock on this system get updated every %.3fms' %
       (GetSystemTimeAdjustment()[0] * 10e3))

On my WinXP system this prints: 15.625ms.

> First column is time in seconds, second is kernel time, third is user time.
> You will notice user time jumping from 3.328 to 3.342 to 3.359, from which I deduce that its resolution is something like 15ms or 60Hz.  

Using these APIs, we can get an accuracy of +/- 15.6ms on each
measurement, e.g. if a test tun takes 1 second, we have an accuracy
of +/- 1.56%.

Do you get stable timings using these APIs ?

Thanks,
-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jun 06 2006)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::


More information about the Python-checkins mailing list