Skip to content

Clarification on cProfile's ".001 seconds" Clock Tick Statement #136902

@HiroIshida

Description

@HiroIshida

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

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy