time.clock()

Benjamin Niemann pink at odahoda.de
Fri Jul 14 12:55:44 EDT 2006


Tobiah wrote:

> The manual says:
> 
> On Unix, return the current processor time as a
> floating point number expressed in seconds.
> 
> So I ran this program:
> 
> #!/usr/bin/python
> 
> import time
> 
> while 1:
>         print time.clock()
> 
> 
> 
> This gave me a stream of floats, the integer part of which
> only updated about every three seconds.  Now, the manual
> also states:
> 
> The precision, and in fact the very definition of the meaning
> of ``processor time'', depends on that of the C function of the same name
> 
> So I "man 3 clock" and notice:
> 
> The value returned is the CPU time used so far as a clock_t; to get  the 
> number
>        of  seconds  used,  divide by CLOCKS_PER_SEC.
> 
> So, I'm wondering how to get that value from python.  All I
> really want to do is know current time relative to a given
> point so that I can capture MIDI events, and store the time
> at which they arrive.  Am I barking up the wrong tree?

What you want sound like the 'wall clock' time. The CPU time is the time
that the CPU spent on executing your process. And unless the process uses
100% of the CPU, CPU time will appear to be 'slower' than the wall clock.
In your little program above the CPU spent about one third of the time on
this process and the rest is used for other processes (e.g. updating the
display).

What you need is time.time(), if its precision is sufficient.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/



More information about the Python-list mailing list