From 5a0078520e80579307124aca567a70e195637682 Mon Sep 17 00:00:00 2001 From: Nathan Froyd Date: Thu, 13 Apr 2017 21:33:24 -0400 Subject: [PATCH] num: add minimal benchmarks for full floating-point formatting We have benchmarks for the floating-point formatting algorithms themselves, but not for the surrounding machinery like Formatter and translating to the flt2dec::Part slices. --- src/libcore/benches/num/flt2dec/mod.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/libcore/benches/num/flt2dec/mod.rs b/src/libcore/benches/num/flt2dec/mod.rs index 1de2bf4921f..7f3b98a1c76 100644 --- a/src/libcore/benches/num/flt2dec/mod.rs +++ b/src/libcore/benches/num/flt2dec/mod.rs @@ -13,6 +13,10 @@ mod strategy { mod grisu; } +use std::f64; +use std::io::Write; +use std::vec::Vec; +use test::Bencher; use core::num::flt2dec::{decode, DecodableFloat, FullDecoded, Decoded}; use core::num::flt2dec::MAX_SIG_DIGITS; @@ -22,3 +26,23 @@ pub fn decode_finite(v: T) -> Decoded { full_decoded => panic!("expected finite, got {:?} instead", full_decoded) } } + +#[bench] +fn bench_small_shortest(b: &mut Bencher) { + let mut buf = Vec::with_capacity(20); + + b.iter(|| { + buf.clear(); + write!(&mut buf, "{}", 3.1415926f64).unwrap() + }); +} + +#[bench] +fn bench_big_shortest(b: &mut Bencher) { + let mut buf = Vec::with_capacity(300); + + b.iter(|| { + buf.clear(); + write!(&mut buf, "{}", f64::MAX).unwrap() + }); +}