kala 2.0.0

Timing with nanosecond precision on multiple architectures.
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented1 out of 1 items with examples
  • Size
  • Source code size: 65.5 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 309.1 kB 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: 3s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Valloric

kala provides a simple API for timing sections of code with sub-nanosecond precision. It can run and time a function while preventing interference from CPU and compiler instruction reordering. It uses CPU hardware clock counters like the Time Stamp Counter (TSC) on x86-64 or the Cycle Counter Register (CCNT) on AArch64 (with an std::time::Instant fallback on other architectures).

use kala::Stopwatch;

let sw = Stopwatch::new(); // Most efficient to create once and re-use.
let nanos: f64 = sw.time(|| 2 + 3).to_nanos(); // Runs closure _once_.
println!("Function took {nanos:.2} nanos");

kala is not a benchmarking harness; it won't run a closure in a loop and report summary statistics. You need to provide that on top of kala.

See the API documentation.

Comparison With Other Libraries

  • quanta
    • Does not prevent interference from CPU and compiler instruction reordering.
    • Less precise TSC frequency estimation.
  • clocksource
    • Does not prevent interference from CPU and compiler instruction reordering.
    • Does not appear to use CPU hardware clocks; always goes through OS APIs.
  • libcpucycles
    • Focused on counting cycles, not time[^1].
    • C library.

The are other libraries in the same space, but all of them either don't use CPU hardware clocks, don't handle CPU/compiler instruction reordering or usually both.

[^1]: CPU cycles don't map cleanly to time due to factors like dynamic frequency scaling, memory latency and others.

Contributing

Every part of this library was written by hand by a professional software engineer with decades of experience. No AI-authored code, text or images are included.

This project rejects all contributions containing AI content. There are no exceptions.