nvzqz / divan

Fast and simple benchmarking for Rust projects
https://nikolaivazquez.com/blog/divan/
Apache License 2.0
849 stars 24 forks source link

Using `with_inputs` or not #41

Closed cBournhonesque closed 6 months ago

cBournhonesque commented 6 months ago

Hi,

I'm not sure I understand the use of with_inputs.

Let's say I have this benchmark:

#[divan::bench(
    types = [Ascii, Unicode],
    consts = LENS,
)]
fn to_ascii_uppercase<G: GenString, const N: usize>(bencher: Bencher) {
    let mut gen = G::default();
    bencher
        .counter(CharsCount::new(N))
        .with_inputs(|| gen.gen_string(N))
        .input_counter(BytesCount::of_str)
        .bench_local_refs(|s| s.to_ascii_uppercase());
}

I get that the time to create the strings wouldn't affect the benchmark.

But what about if I create the string inside bench_local, like this?

#[divan::bench(
    types = [Ascii, Unicode],
    consts = LENS,
)]
fn to_ascii_uppercase<G: GenString, const N: usize>(bencher: Bencher) {
    let mut gen = G::default();
    let s = gen.gen_string(N)
    bencher
        .bench_local(|| s.to_ascii_uppercase());
}

The time to create the string is also not included in the benchmark right? Therefore is the use of with_inputs mostly to have additional helpers such as input_counter?

nvzqz commented 6 months ago

You are correct that the first and second example do not include string generation time in benchmark timings. However, that's where the similarities end.

Differences: