Expand description
A zero-dependency, high-performance time measurement library for Rust.
This library provides nanosecond precision benchmarking with true zero-overhead when disabled. Designed as a foundational primitive that other libraries can depend on without concern for bloat, version conflicts, or performance impact.
§Features
- Zero Dependencies: Built using only the Rust standard library
- True Zero Overhead: When disabled, all code compiles away completely
- Thread Safe: Core functions are pure, with optional thread-safe collection
- Async Compatible: Works seamlessly with any async runtime
- Simple API: Just four functions and two macros
§Quick Start
use benchmark::{measure, time};
// Measure a function
let (result, duration) = measure(|| {
// Some expensive computation
42
});
println!("Computation took {} nanoseconds", duration.as_nanos());
// Use the macro for convenience
fn expensive_function() -> i32 { 2 + 2 }
let (result, duration) = time!(expensive_function());
assert_eq!(result, 4);
§Production Metrics (feature = “metrics”)
The following examples compile only when features = ["std", "metrics"]
are enabled.
use benchmark::{stopwatch, Watch};
fn main() {
let watch = Watch::new();
stopwatch!(watch, "work", {
// do work
});
let s = &watch.snapshot()["work"];
assert!(s.count >= 1);
}
Modules§
- histogram
- High-Performance Histogram
Macros§
- benchmark
- Macro-benchmark an expression/function for a number of iterations and return
the last result together with raw per-iteration
Measurement
s. - benchmark_
block - Micro-benchmark a code block for a number of iterations and return raw per-iteration durations.
- stopwatch
- Disabled version of
stopwatch!
whenmetrics
is off. - time
- Times an expression and returns (result, duration).
- time_
named - Times an expression with a name and returns (result, measurement).
Structs§
- Collector
- A thread-safe collector for measurements.
- Duration
- A duration represented in nanoseconds.
- Measurement
- A single time measurement.
- Stats
- Basic statistics for a set of measurements.
Functions§
- measure
- Measures the execution time of a function.
- measure_
named - Measures the execution time of a function with a name.