1 #![feature(test)]
2 #![allow(non_snake_case)]
3 #![allow(clippy::cast_lossless)]
4 
5 extern crate test;
6 
7 macro_rules! benches {
8     ($($name:ident($value:expr))*) => {
9         mod bench_itoa_format {
10             use test::{Bencher, black_box};
11 
12             $(
13                 #[bench]
14                 fn $name(b: &mut Bencher) {
15                     let mut buffer = itoa::Buffer::new();
16 
17                     b.iter(|| {
18                         let printed = buffer.format(black_box($value));
19                         black_box(printed);
20                     });
21                 }
22             )*
23         }
24 
25         mod bench_std_fmt {
26             use std::io::Write;
27             use test::{Bencher, black_box};
28 
29             $(
30                 #[bench]
31                 fn $name(b: &mut Bencher) {
32                     let mut buf = Vec::with_capacity(40);
33 
34                     b.iter(|| {
35                         buf.clear();
36                         write!(&mut buf, "{}", black_box($value)).unwrap();
37                         black_box(&buf);
38                     });
39                 }
40             )*
41         }
42     }
43 }
44 
45 benches! {
46     bench_u64_0(0u64)
47     bench_u64_half(u32::max_value() as u64)
48     bench_u64_max(u64::max_value())
49 
50     bench_i16_0(0i16)
51     bench_i16_min(i16::min_value())
52 
53     bench_u128_0(0u128)
54     bench_u128_max(u128::max_value())
55 }
56