Yi Kong | 8884bbe | 2020-08-31 01:13:13 +0800 | [diff] [blame] | 1 | #![cfg_attr(feature = "cargo-clippy", allow(cast_lossless))] |
| 2 | #![feature(test)] |
| 3 | #![allow(non_snake_case)] |
| 4 | |
| 5 | extern crate itoa; |
| 6 | extern crate test; |
| 7 | |
| 8 | macro_rules! benches { |
| 9 | ( |
| 10 | $( |
| 11 | $(#[$attr:meta])* |
| 12 | $name:ident($value:expr) |
| 13 | ),* |
| 14 | ) => { |
| 15 | mod bench_itoa_write { |
| 16 | use test::{Bencher, black_box}; |
| 17 | $( |
| 18 | $(#[$attr])* |
| 19 | #[bench] |
| 20 | fn $name(b: &mut Bencher) { |
| 21 | use itoa; |
| 22 | |
| 23 | let mut buf = Vec::with_capacity(40); |
| 24 | |
| 25 | b.iter(|| { |
| 26 | buf.clear(); |
| 27 | itoa::write(&mut buf, black_box($value)).unwrap() |
| 28 | }); |
| 29 | } |
| 30 | )* |
| 31 | } |
| 32 | |
| 33 | mod bench_itoa_fmt { |
| 34 | use test::{Bencher, black_box}; |
| 35 | $( |
| 36 | $(#[$attr])* |
| 37 | #[bench] |
| 38 | fn $name(b: &mut Bencher) { |
| 39 | use itoa; |
| 40 | |
| 41 | let mut buf = String::with_capacity(40); |
| 42 | |
| 43 | b.iter(|| { |
| 44 | buf.clear(); |
| 45 | itoa::fmt(&mut buf, black_box($value)).unwrap() |
| 46 | }); |
| 47 | } |
| 48 | )* |
| 49 | } |
| 50 | |
| 51 | mod bench_std_fmt { |
| 52 | use test::{Bencher, black_box}; |
| 53 | $( |
| 54 | $(#[$attr])* |
| 55 | #[bench] |
| 56 | fn $name(b: &mut Bencher) { |
| 57 | use std::io::Write; |
| 58 | |
| 59 | let mut buf = Vec::with_capacity(40); |
| 60 | |
| 61 | b.iter(|| { |
| 62 | buf.clear(); |
| 63 | write!(&mut buf, "{}", black_box($value)).unwrap() |
| 64 | }); |
| 65 | } |
| 66 | )* |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
Haibo Huang | a4c5ece | 2020-12-29 20:32:47 -0800 | [diff] [blame] | 71 | benches! { |
Yi Kong | 8884bbe | 2020-08-31 01:13:13 +0800 | [diff] [blame] | 72 | bench_u64_0(0u64), |
| 73 | bench_u64_half(<u32>::max_value() as u64), |
| 74 | bench_u64_max(<u64>::max_value()), |
| 75 | |
| 76 | bench_i16_0(0i16), |
| 77 | bench_i16_min(<i16>::min_value()), |
| 78 | |
| 79 | #[cfg(feature = "i128")] |
| 80 | bench_u128_0(0u128), |
| 81 | #[cfg(feature = "i128")] |
| 82 | bench_u128_max(<u128>::max_value()) |
| 83 | } |