1 // cargo bench 2 3 #![feature(test)] 4 5 extern crate test; 6 7 use std::io::Write; 8 use std::{f32, f64}; 9 use test::{black_box, Bencher}; 10 11 macro_rules! benches { 12 ($($name:ident($value:expr),)*) => { 13 mod bench_ryu { 14 use super::*; 15 $( 16 #[bench] 17 fn $name(b: &mut Bencher) { 18 let mut buf = ryu::Buffer::new(); 19 20 b.iter(move || { 21 let value = black_box($value); 22 let formatted = buf.format_finite(value); 23 black_box(formatted); 24 }); 25 } 26 )* 27 } 28 29 mod bench_std_fmt { 30 use super::*; 31 $( 32 #[bench] 33 fn $name(b: &mut Bencher) { 34 let mut buf = Vec::with_capacity(20); 35 36 b.iter(|| { 37 buf.clear(); 38 let value = black_box($value); 39 write!(&mut buf, "{}", value).unwrap(); 40 black_box(buf.as_slice()); 41 }); 42 } 43 )* 44 } 45 }; 46 } 47 48 benches! { 49 bench_0_f64(0f64), 50 bench_short_f64(0.1234f64), 51 bench_e_f64(2.718281828459045f64), 52 bench_max_f64(f64::MAX), 53 bench_0_f32(0f32), 54 bench_short_f32(0.1234f32), 55 bench_e_f32(2.718281828459045f32), 56 bench_max_f32(f32::MAX), 57 } 58