CPU time

Skip Montanaro skip at pobox.com
Fri Oct 12 18:17:02 EDT 2001


    >> I thought time.clock() measures CPU time which (i assume) is
    >> consistent regardless of other S/W running.. but somehow the
    >> measurement fluctuates a lot. Am I misunderstanding this function? If
    >> so, what is the function w/ the functionality I'm looking for?

What platform are you using?  On systems having the clock(3) library call,
time.clock does measure CPU time consumed by the current process, assuming
clock(3) does (it does on my system), but with varying degrees of precision
(generally not a lot).  In addition, POSIX requires that the return value of
clock(3) be in microseconds, so if you are measuring a long-running process
the value returned by time.clock can wrap (according to the man page on my
Linux sytem, wraparound occurs approximately every 72 minutes).

If you are using Win32, time.clock uses a Windows routine called
QueryPerformanceCounter.  I have no direct experience with it, but I am told
that it returns a value that is independent of what process has the
processor.  Others I'm sure can offer more definitive information about this
routine.  For more information on QueryPerformanceCounter usage in Python,
try Google:

    http://groups.google.com/groups?group=comp.lang.python.%2A&q=QueryPerformanceCounter

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list