Skip to content

ERROR: At least one measurement of benchmark {} took zero time per iteration. #862

@zooko

Description

@zooko

I was getting this error when benchmarking my code, so I boiled it down to a minimal project that reproduces the same error message. The key seems to be if the setup function (for iter_batched) takes a long time, but the routine itself takes little time, then I get this error message. The following code generates the error message on my machine (CPU: Apple M4 Max) but if I reduce the SETUP const from 1_000_000 to 100_000 then the error message stops appearing.

use criterion::{criterion_group, criterion_main, Criterion, BatchSize};
use std::hint::black_box;

const SETUP: i32 = 1_000_000;
fn test_criterion(c: &mut Criterion) {
    let setup = || {
        let mut i = 0;
        let mut x = 1;
        while i < SETUP {
            x += black_box(i * x);
            i += 1;
        }
        black_box(i)
    };

    let f = |i| {
        let mut j = 0;
        let mut x = 1;
        while j < 2 {
            x += black_box(i * x + j);
            j += black_box(1);
        }
    };

    c.bench_function("test_criterion", move |b| b.iter_batched(setup, f, BatchSize::SmallInput));
}

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions