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 Stopwatch;
let sw = new; // Most efficient to create once and re-use.
let nanos: f64 = sw.time.to_nanos; // Runs closure _once_.
println!;
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.
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
The source code in 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.