| 1 |
use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 |
use std::fmt::Display; |
| 3 |
use std::hint; |
| 4 |
use std::io::Write; |
| 5 |
|
| 6 |
fn do_bench(c: &mut Criterion, group_name: &str, int: impl itoa::Integer + Display) { |
| 7 |
let mut group = c.benchmark_group(group_name); |
| 8 |
group.bench_function("itoa", |b| { |
| 9 |
let mut buf = itoa::Buffer::new(); |
| 10 |
b.iter(move || { |
| 11 |
let int = hint::black_box(int); |
| 12 |
let formatted = buf.format(int); |
| 13 |
hint::black_box(formatted); |
| 14 |
}); |
| 15 |
}); |
| 16 |
group.bench_function("std::fmt", |b| { |
| 17 |
let mut buf = Vec::with_capacity(20); |
| 18 |
b.iter(|| { |
| 19 |
buf.clear(); |
| 20 |
let int = hint::black_box(int); |
| 21 |
write!(&mut buf, "{int}").unwrap(); |
| 22 |
hint::black_box(buf.as_slice()); |
| 23 |
}); |
| 24 |
}); |
| 25 |
group.finish(); |
| 26 |
} |
| 27 |
|
| 28 |
fn bench(c: &mut Criterion) { |
| 29 |
do_bench(c, "u64[0]", 0u64); |
| 30 |
do_bench(c, "u64[half]", u64::from(u32::MAX)); |
| 31 |
do_bench(c, "u64[max]", u64::MAX); |
| 32 |
|
| 33 |
do_bench(c, "i16[0]", 0i16); |
| 34 |
do_bench(c, "i16[min]", i16::MIN); |
| 35 |
|
| 36 |
do_bench(c, "u128[0]", 0u128); |
| 37 |
do_bench(c, "u128[max]", u128::MAX); |
| 38 |
} |
| 39 |
|
| 40 |
criterion_group!(benches, bench); |
| 41 |
criterion_main!(benches); |