cheatah
Module

time

High-accuracy timing built on C++ <chrono> clocks (system_clock for wall-clock, steady_clock for monotonic counters).

import time

start = time.perf_counter()
time.sleep(0.5)
elapsed = time.perf_counter() - start

Functions

Wall-clock (since the Unix epoch):

  • time() — seconds as a double.

  • time_ns() — nanoseconds as a long long.

Monotonic (never runs backwards):

  • monotonic() / monotonic_ns() — monotonic seconds / nanoseconds.

  • perf_counter() / perf_counter_ns() — highest-resolution monotonic counter.

Other:

  • process_time() — CPU time consumed by this process, in seconds.

  • sleep(seconds) — suspend the current thread for a (fractional) duration.

Per-function docs (parameters, runtime complexity, heap behavior) are in time.hpp. Tested in ../tests/time_test.cpp; ASan + Valgrind clean via the QA gate (security/run-valgrind.sh).

Functions

fn double time() source#

Wall-clock time.

Returns fractional seconds since the Unix epoch (1970-01-01 UTC) from the system clock; because it follows real time, it can jump forward or backward when the clock is adjusted (NTP, manual changes) and is not suitable for measuring elapsed intervals.

Returns

seconds since the Unix epoch (system_clock).

Complexity

O(1) time.

Allocation

none.

Compile-run testTimeCompileRun.Time
System testStdlibE2E.Time
Performance18.12 ns/call in cheatah · 65.66 ns/call in CPython 3.12.3 · ≈3.6× faster
fn long long time_ns() source#

Wall-clock time.

Same wall clock as time but returned as an integer count of nanoseconds since the Unix epoch, avoiding the precision loss of double seconds; it shares the same caveat that the system clock can be stepped backward.

Returns

nanoseconds since the Unix epoch (system_clock).

Complexity

O(1) time.

Allocation

none.

Compile-run testTimeCompileRun.TimeNs
System testStdlibE2E.Time
Performance15.22 ns/call in cheatah · 87.65 ns/call in CPython 3.12.3 · ≈5.8× faster
fn double monotonic() source#

Monotonic clock; never runs backwards.

Returns fractional seconds from steady_clock, which advances steadily and is immune to system-clock adjustments, making it the right choice for timing intervals; its zero point is unspecified, so only differences are meaningful.

Returns

seconds from steady_clock.

Complexity

O(1) time.

Allocation

none.

Compile-run testTimeCompileRun.Monotonic
System testStdlibE2E.Time
Performance18.94 ns/call in cheatah · 63.91 ns/call in CPython 3.12.3 · ≈3.4× faster
fn long long monotonic_ns() source#

Monotonic clock; never runs backwards.

Same monotonic steady_clock as monotonic but as an integer nanosecond count, preserving full precision; only differences between readings have a defined meaning.

Returns

nanoseconds from steady_clock.

Complexity

O(1) time.

Allocation

none.

System testStdlibE2E.Time
Performance14.63 ns/call in cheatah · 82.16 ns/call in CPython 3.12.3 · ≈5.7× faster
fn double perf_counter() source#

Highest-resolution monotonic counter.

Intended as the finest-grained clock for benchmarking; in this build it is backed by the same steady_clock as monotonic, so it is monotonic with an unspecified origin and should be used only for elapsed-time measurements.

Returns

seconds from steady_clock.

Complexity

O(1) time.

Allocation

none.

Performance19.09 ns/call in cheatah · 64.31 ns/call in CPython 3.12.3 · ≈3.4× faster
fn long long perf_counter_ns() source#

Highest-resolution monotonic counter.

Nanosecond-precision form of perf_counter, backed by steady_clock; like the other monotonic readings, only the difference between two calls is meaningful.

Returns

nanoseconds from steady_clock.

Complexity

O(1) time.

Allocation

none.

System testStdlibE2E.Time
Performance15.83 ns/call in cheatah · 84.19 ns/call in CPython 3.12.3 · ≈5.3× faster
fn double process_time() source#

CPU time consumed by this process.

Returns processor time used by the program (via std::clock / CLOCKS_PER_SEC), not wall-clock time, so it excludes time spent sleeping or blocked and may grow faster than real time across multiple threads.

Returns

seconds of CPU time (std::clock).

Complexity

O(1) time.

Allocation

none.

System testStdlibE2E.Time
Performance160 ns/call in cheatah · 237 ns/call in CPython 3.12.3 · ≈1.5× faster
fn void sleep(double seconds) source#

Suspend the calling thread.

Blocks the current thread for at least seconds (fractional values are honored); the OS may sleep slightly longer due to scheduling, and a non-positive duration returns essentially immediately.

Parameters
seconds

duration to sleep (fractional).

Complexity

O(1) plus the sleep wait.

Allocation

none.

Concurrency

blocks only the calling thread; other threads keep running.

Compile-run testTimeCompileRun.Sleep
Performanceblocks for a requested duration — not micro-benchmarked