fast-clock 0.2.0

Low-overhead timing without hiding the details from you
Documentation
  • Coverage
  • 65.12%
    28 out of 43 items documented1 out of 33 items with examples
  • Size
  • Source code size: 26.61 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.2 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • m-mueller678/fast-clock
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • m-mueller678

fast-clock

Low-overhead timing for benchmarking without hiding the details from you.

Most timing crates wrap the clocks behind a convenient but opaque API. fast-clock exposes the raw time source directly and offers some utilities to make them easier to work with. There is no global state, no automatic fallbacks, no automatic calibration, and no automatic conversion to std's Duration. fast-clock supports no-std, no-alloc, and targets without floating-point support.

Clocks

TSC (x86_64)

The x86_64 timestamp counter can be read with a single instruction. The frequency of the TSC varies by CPU, and for some CPUs even changes over time or between cores. On Linux, fast-clock provides functionality to check whether the TSC frequency is constant.

// On Linux, verifies the kernel considers the TSC stable across cores.
let tsc = Tsc::try_new_linux_sys().unwrap();

// Calibrate against std::time::Instant (sleeps ~10 ms once at startup).
let (calibration, sync) = U64Calibration::new_with_std_instant(
    &tsc,
    std::time::Duration::from_millis(10),
);

let t0 = tsc.now();
// ... timed section ...
let t1 = tsc.now();

let ticks = WrappingU64Time::instant_sub(t1, t0);
let ns: u64 = calibration.convert_to_ns(ticks);

Converting between clock domains

The sync value returned by the calibration in the previous example is a ClockSynchronization that maps instants between the TSC domain and std::time::Instant. This is useful when correlating TSC measurements with wall-clock timestamps or other clocks.

// Convert a TSC instant to std::time::Instant.
let wall = sync.to_a(t0, &InherentlyCalibrated, &calibration);

ClockSynchronization is general: it works between any two clock domains.

std::time::Instant / SystemTime

std_clocks::InstantClock and std_clocks::SystemClock wrap the standard library clocks. Their durations are already in nanoseconds, so no calibration is needed.

Contributions

Contributions adding more clocks are welcome.