Skip to main content

run_closure_with_setup

Function run_closure_with_setup 

Source
pub fn run_closure_with_setup<S, T, F>(
    spec: BenchSpec,
    setup: S,
    f: F,
) -> Result<BenchReport, TimingError>
where S: FnOnce() -> T, F: FnMut(&T) -> Result<(), TimingError>,
Expand description

Runs a benchmark with setup that executes once before all iterations.

The setup function is called once before timing begins, then the benchmark runs multiple times using a reference to the setup result. This is useful for expensive initialization that shouldn’t be included in timing.

§Arguments

  • spec - Benchmark configuration specifying iterations and warmup
  • setup - Function that creates the input data (called once, not timed)
  • f - Benchmark closure that receives a reference to setup result

§Example

use mobench_sdk::timing::{BenchSpec, run_closure_with_setup};

fn setup_data() -> Vec<u8> {
    vec![0u8; 1_000_000]  // Expensive allocation not measured
}

let spec = BenchSpec::new("hash_benchmark", 100, 10)?;
let report = run_closure_with_setup(spec, setup_data, |data| {
    std::hint::black_box(compute_hash(data));
    Ok(())
})?;