12

I am developing some benchmarks for a crate using criterion (cargo bench). I would like to temporarily limit the amount of iterations until I finish the code.

I know measurements may not be precise, but this is just temporary. Is this possible?

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Juan Leni
  • 6,982
  • 5
  • 55
  • 87

1 Answers1

7

It is possible.

Look at the Criterion Benchmark. There you can find methods relevant to you, specifically measurement_time .

Digging deeper, you can find how to use them here:

fn bench(c: &mut Criterion) {
    // Setup (construct data, allocate memory, etc)
    c.bench(
        "routines",
        Benchmark::new("routine_1", |b| b.iter(|| routine_1()))
            .with_function("routine_2", |b| b.iter(|| routine_2()))
            .measurement_time(Duration::from_millis(1000))
    );
}

criterion_group!(benches, bench);
criterion_main!(benches);

Where measurement_time(Duration::from_millis(1000)) is the droid you're looking for. This effectively dropped the number of iterations for my specific function by 80%.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
Leśny Rumcajs
  • 2,259
  • 2
  • 17
  • 33