time.clock()

T

Tobiah

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
 
E

El Duderino

Tobiah said:
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.
 
B

Benjamin Niemann

Tobiah said:
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.
 
D

Dennis Lee Bieber

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?
Well, there is always the "compute it" method, though that may not
be the most accurate...

(the sleeper).... t0 = time.clock()
.... time.sleep(20.0)
.... t1 = time.clock()
.... print t1 - t0
.... print (t1 - t0) / 20.0
.... 19.9854360983
0.999271804915

(the busy-wait).... c0 = time.clock(); t0 = time.time()
.... while (time.time() - t0) < 20.0: pass
.... c1 = time.clock()
.... print c1 - c0
.... print (c1 - c0) / 20.0
.... 19.9918087902
0.999590439512
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
F

Fredrik Lundh

Tobiah said:
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>
 
G

Grant Edwards

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.
 
N

Nick Craig-Wood

Benjamin Niemann said:
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.
....
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
....
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top