itoa/benches/bench.rs

62 lines
1.5 KiB
Rust
Raw Normal View History

2016-06-25 17:32:23 -04:00
#![feature(test)]
#![allow(non_snake_case)]
#![allow(clippy::cast_lossless)]
2016-06-25 17:32:23 -04:00
extern crate test;
macro_rules! benches {
2017-09-12 16:52:24 -04:00
(
$(
$(#[$attr:meta])*
$name:ident($value:expr)
),*
) => {
mod bench_itoa_format {
2016-06-25 17:32:23 -04:00
use test::{Bencher, black_box};
$(
2017-09-12 16:52:24 -04:00
$(#[$attr])*
2016-06-25 17:32:23 -04:00
#[bench]
fn $name(b: &mut Bencher) {
let mut buffer = itoa::Buffer::new();
2016-06-25 17:32:23 -04:00
b.iter(|| {
let printed = buffer.format(black_box($value));
black_box(printed);
2018-03-18 03:25:02 -04:00
});
}
)*
}
mod bench_std_fmt {
2016-06-25 17:32:23 -04:00
use test::{Bencher, black_box};
$(
2017-09-12 16:52:24 -04:00
$(#[$attr])*
2016-06-25 17:32:23 -04:00
#[bench]
fn $name(b: &mut Bencher) {
use std::io::Write;
2017-09-12 16:52:24 -04:00
let mut buf = Vec::with_capacity(40);
2016-06-25 17:32:23 -04:00
b.iter(|| {
buf.clear();
write!(&mut buf, "{}", black_box($value)).unwrap();
black_box(&buf);
2016-06-25 17:32:23 -04:00
});
}
)*
}
}
}
2020-06-28 22:00:26 -04:00
benches! {
2017-09-12 16:52:24 -04:00
bench_u64_0(0u64),
bench_u64_half(<u32>::max_value() as u64),
bench_u64_max(<u64>::max_value()),
2016-06-25 17:32:23 -04:00
2017-09-12 16:52:24 -04:00
bench_i16_0(0i16),
bench_i16_min(<i16>::min_value()),
bench_u128_0(0u128),
bench_u128_max(<u128>::max_value())
}