]> git.lizzy.rs Git - rust.git/commitdiff
num: add minimal benchmarks for full floating-point formatting
authorNathan Froyd <froydnj@gmail.com>
Fri, 14 Apr 2017 01:33:24 +0000 (21:33 -0400)
committerNathan Froyd <froydnj@gmail.com>
Fri, 28 Apr 2017 19:24:09 +0000 (15:24 -0400)
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

index 1de2bf4921f589996130885f363b550cbf179695..7f3b98a1c7614f251452cd72df7dffe78096655e 100644 (file)
@@ -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<T: DecodableFloat>(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()
+    });
+}