macro_rules! timed {
($label:expr, $callback:expr, $block:expr) => { ... };
}Expand description
Times the execution of a block and invokes a callback with the label and elapsed duration.
In debug builds (debug_assertions enabled), this macro:
- Records the start time
- Executes the block
- Calculates elapsed time
- Calls the callback with
(label, duration) - Returns the block’s result
In release builds, the timing is completely eliminated — only the block executes, with zero overhead.
§Arguments
$label- A label (any type accepted by the callback) identifying what is being timed$callback- A closure or function:FnOnce(label, Duration)$block- The code block to time
§Examples
ⓘ
use varvedb::timed;
use std::time::Duration;
let result = timed!("my_operation", |label, dur: Duration| {
eprintln!("[{label}] took {dur:?}");
}, {
// expensive work here
42
});
assert_eq!(result, 42);Using a named function as callback:
ⓘ
fn log_timing(label: &str, duration: std::time::Duration) {
eprintln!("[timing] {}: {:?}", label, duration);
}
let value = timed!("computation", log_timing, {
(0..1000).sum::<i32>()
});