calculating system clock resolution
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Sat Apr 8 07:45:11 EDT 2006
On Sat, 08 Apr 2006 04:16:20 -0700, jUrner wrote:
> def calc_time_res():
> now = time.time
> start = now()
> x = start
> while start == x:
> x = now()
> print x, start # <--
> print x - start
>
> print calc_time_res()
>>> 1.50203704834e-05
>
> Something is going wrong here.
> If you look at the function ,time.time() returns time in microseconds
> (most oses and so does mine).
>From help(time.time):
time() -> floating point number
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.
Seconds, not microseconds.
> So the calculation goes, lets say 1.23 - 1.24
How can the time *after* the while loop be less than the time *before* the
while loop?
> How can the result be something like 1.50203704834e-05 ?
Have you looked at the output of time.time()?
>>> time.time()
1144496209.3221531
Of course, Python only prints a certain number of digits; to see the full
amount, use string formatting:
>>> '%40.30f' % time.time()
'1144496642.905987024307250976562500000000'
This brings me to an even simpler method of getting the resolution of
time.time(), without the overhead of a while loop:
>>> abs(time.time() - time.time())
1.0013580322265625e-05
which is approximately 0.01ms, just as you expected.
--
Steven.
More information about the Python-list
mailing list