-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Description
Documentation
@birkenfeld
While reading the cProfile
documentation's Limitations section, I came across the statement
The most obvious restriction is that the underlying “clock” is only ticking at a rate (typically) of about .001 seconds.
However, after examining the cProfile source code, it appears that on Linux systems, the internal clock used is clock_gettime(CLOCK_MONOTONIC_RAW, ...). Based on a helpful replies to my Python Discourse post, I ran an experiment that suggests the resolution of CLOCK_MONOTONIC_RAW is actually around 2 microseconds — several orders of magnitude more precise than the stated 0.001 seconds.
The discrepancy was a little confusing, and I feel that the documentation could perhaps benefit from some additional clarification on the tick rate and clock. Also, because the documentation seem to be written in 18 years ago, and the value might be outdated.
import time
for _ in range(10):
time.sleep(0.01)
clock = time.CLOCK_MONOTONIC_RAW
t1 = time.clock_gettime_ns(clock)
while True:
t2 = time.clock_gettime_ns(clock)
if t2 != t1:
break
delta_ns = t2 - t1
print(f"Observed tick: {delta_ns} ns")
h-ishida@umejuice:~/tmp/tmp$ python3 tmp.py
Observed tick: 2274 ns
Observed tick: 2084 ns
Observed tick: 1522 ns
Observed tick: 2324 ns
Observed tick: 2064 ns
Observed tick: 2013 ns
Observed tick: 2064 ns
Observed tick: 1222 ns
Observed tick: 1773 ns
Observed tick: 2695 ns
Metadata
Metadata
Assignees
Labels
Projects
Status