Sub topic 9
Reporting throughput in bytes or elements
This chapter assumes you can write a basic Criterion benchmark, use black_box, and use benchmark groups with BenchmarkId. You should be able to read the time: line in Criterion's console output.
Why time alone is not enough
A time per iteration answers "how long does one call take?". When a function processes variable-sized input, that number is hard to compare. Hashing 1 KiB in 500 ns and hashing 256 KiB in 120 µs are different workloads, and the raw times don't show which implementation scales better.
Throughput divides the amount of work by the time. You get a figure such as GiB/s or Melem/s. A flat throughput across input sizes means the code scales linearly. A drop at large sizes often points to cache effects.
The two units
You tell Criterion how much work one iteration does with the Throughput enum.
| Variant | Meaning | Reported as |
|---|---|---|
| `Throughput::Bytes(n)` | One iteration processes `n` bytes | B/s, KiB/s, MiB/s, GiB/s |
| `Throughput::BytesDecimal(n)` | Same, but with decimal units | kB/s, MB/s, GB/s |
| `Throughput::Elements(n)` | One iteration processes `n` items | elem/s, Kelem/s, Melem/s |
Use Bytes for buffers, parsers, and codecs. Use Elements when the natural unit is a record, a request, or a collection entry.
Setting throughput on a group
Throughput is a property of a benchmark group, so you set it with group.throughput(...). The setting applies to every benchmark you register after it, until you change it. Because the amount of work usually depends on the input, set it inside the loop, before each bench_with_input call.
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
fn checksum(data: &[u8]) -> u32 {
data.iter().fold(0u32, |acc, &b| acc.wrapping_add(b as u32))
}
fn bench_checksum(c: &mut Criterion) {
let mut group = c.benchmark_group("checksum");
for size in [1_024usize, 16_384, 262_144] {
let data = vec![7u8; size];
group.throughput(Throughput::Bytes(size as u64));
group.bench_with_input(BenchmarkId::from_parameter(size), &data, |b, data| {
b.iter(|| checksum(black_box(data)))
});
}
group.finish();
}
criterion_group!(benches, bench_checksum);
criterion_main!(benches);Run it with cargo bench. Each benchmark now prints a second line under time:. The numbers below are illustrative, not measured:
checksum/16384 time: [8.1 µs 8.2 µs 8.3 µs]
thrpt: [1.84 GiB/s 1.86 GiB/s 1.88 GiB/s]The thrpt: line has the same three-part shape as time:: lower bound, estimate, upper bound. The bounds swap order because a slower time means lower throughput.
Counting elements
When each iteration handles a collection, pass its length.
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
fn double_all(items: &[u64]) -> Vec<u64> {
items.iter().map(|x| x * 2).collect()
}
fn bench_double(c: &mut Criterion) {
let items: Vec<u64> = (0..10_000).collect();
let mut group = c.benchmark_group("double_all");
group.throughput(Throughput::Elements(items.len() as u64));
group.bench_function("10k items", |b| b.iter(|| double_all(black_box(&items))));
group.finish();
}
criterion_group!(benches, bench_double);
criterion_main!(benches);The report shows how many elements per second the function handles, which is easier to reason about than the time for a 10,000-item batch.
Common mistakes
- Setting throughput once outside the loop, so every size reports the same work.
- Forgetting
as u64. The variants takeu64, notusize. - Counting setup work. If you use
iter_batched, count only what the measured closure processes. - Mixing
BytesandBytesDecimalacross runs. Baseline comparisons work on time, but the units shown will differ and confuse readers.
Exercise
You benchmark a parser over inputs of 1 KiB, 64 KiB, and 1 MiB inside one group, and the report shows the same throughput line for all three. What is the most likely cause, and how do you fix it?
Show answer
The throughput was most likely set once, outside the loop, to a single value. Call group.throughput(Throughput::Bytes(size as u64)) inside the loop, before each bench_with_input, so every input size declares its own amount of work.
Exercise
Which Throughput variant fits a function that validates a slice of 5,000 records per call, and what do you pass to it?
Show answer
Throughput::Elements(5_000), or Throughput::Elements(records.len() as u64) so the count stays tied to the actual input.