Divan allows you to track metrics like bytes, characters, cycles, or items processed in each iteration of a benchmark. This helps in measuring throughput (e.g., MB/s or items/s).
You can provide a counter in three ways:
- Using the
#[divan::bench(counters = ...)] or #[divan::bench_group(counters = ...)] macros. - Using
Bencher::counter within the benchmark function. - Using
Bencher::input_counter.
Common counter types include:
BytesCount: Measures data in bytes.CharsCount: Measures data in Unicode code points (char).CyclesCount: Measures cycles (displayed as Hertz).ItemsCount: Measures the number of items processed.
use divan::counter::BytesCount;
#[divan::bench]
fn slice_into_vec(bencher: divan::Bencher) {
let ints: &[i32] = &[1, 2, 3];
let bytes = BytesCount::of_slice(ints);
bencher
.counter(bytes)
.bench(|| -> Vec<i32> {
divan::black_box(ints).into()
});
}