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