By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,530 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,530 IT Pros & Developers. It's quick & easy.

time.clock()

P: n/a
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?

Thanks,

Toby

--
Posted via a free Usenet account from http://www.teranews.com

Jul 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Tobiah wrote:
Am I barking up the wrong tree?
I don't think so, time.clock() has always worked fine for me. You can
also try time.time(). It is not as precise, but it might be sufficient
for your needs.
Jul 14 '06 #2

P: n/a
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/
Jul 14 '06 #3

P: n/a
Tobiah wrote:
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.
by calling clock(), of course.
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.
if you want real time, use time.time().

CPU time (processor time) is something different; that's based on how
many CPU cycles your program has used up since it started (usually
approximated by checking what process is running at regular intervals).
if your program doesn't do anything, or, as in your example, spends
most of its time waiting for someone else to do something, it won't
consume many cycles.

</F>

Jul 14 '06 #4

P: n/a
On 2006-07-14, Tobiah <to**@rcsreg.comwrote:
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.
What value?
All I really want to do is know current time relative to a
given point
Which is not at all the same thing as CPU usage.
so that I can capture MIDI events, and store the time at which
they arrive.
time.time()
Am I barking up the wrong tree?
Yes, but your in the right grove.

--
Grant Edwards grante Yow! "THE LITTLE PINK
at FLESH SISTERS," I saw them
visi.com at th' FLUROESCENT BULB
MAKERS CONVENTION...
Jul 14 '06 #5

P: n/a
Benjamin Niemann <pi**@odahoda.dewrote:
Tobiah wrote:
On Unix...
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.
In linux at least time.time() has microsecond precision.
>>for i in range(10): print "%20.6f" % time.time()
....
1153130111.566463
1153130111.566513
1153130111.566535
1153130111.566557
1153130111.566578
1153130111.566601
1153130111.566621
1153130111.566644
1153130111.566665
1153130111.566686

Wheras time.clock() only has 10 ms precision
>>for i in range(10): print "%20.6f" % time.clock()
....
1.770000
1.770000
1.770000
1.770000
1.770000
1.770000
1.770000
1.770000
1.770000
1.770000

time.clock() is elapsed cpu time of just that process.

I think the precisions are the other way round on windows.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.