itoa/benches/bench.rs

66 lines
1.5 KiB
Rust
Raw Normal View History

2017-09-12 16:52:24 -04:00
#![cfg_attr(feature = "i128", feature(i128_type, i128))]
2016-06-25 17:32:23 -04:00
#![feature(test)]
#![allow(non_snake_case)]
extern crate itoa;
extern crate test;
macro_rules! benches {
2017-09-12 16:52:24 -04:00
(
$(
$(#[$attr:meta])*
$name:ident($value:expr)
),*
) => {
2016-06-25 17:32:23 -04:00
mod bench_itoa {
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 itoa;
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();
itoa::write(&mut buf, black_box($value)).unwrap()
});
}
)*
}
mod bench_fmt {
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()
});
}
)*
}
}
}
2017-09-12 16:52:24 -04:00
benches!{
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()),
#[cfg(feature = "i128")]
bench_u128_0(0u128),
#[cfg(feature = "i128")]
bench_u128_max(<u128>::max_value())
}