From 62cfd8a123e2ee78fab6ca35ae8bffded28ab620 Mon Sep 17 00:00:00 2001 From: yukang Date: Tue, 21 Feb 2023 03:26:40 +0000 Subject: [PATCH] fix more benchmark test with black_box --- library/core/benches/fmt.rs | 24 ++++++++++++------------ library/core/benches/num/dec2flt/mod.rs | 24 ++++++++++++------------ library/core/benches/num/flt2dec/mod.rs | 6 +++--- library/core/benches/num/mod.rs | 6 +++--- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/library/core/benches/fmt.rs b/library/core/benches/fmt.rs index ff726ff7559..d1cdb12e50f 100644 --- a/library/core/benches/fmt.rs +++ b/library/core/benches/fmt.rs @@ -1,13 +1,13 @@ use std::fmt::{self, Write as FmtWrite}; use std::io::{self, Write as IoWrite}; -use test::Bencher; +use test::{black_box, Bencher}; #[bench] fn write_vec_value(bh: &mut Bencher) { bh.iter(|| { let mut mem = Vec::new(); for _ in 0..1000 { - mem.write_all("abc".as_bytes()).unwrap(); + mem.write_all(black_box("abc").as_bytes()).unwrap(); } }); } @@ -18,7 +18,7 @@ fn write_vec_ref(bh: &mut Bencher) { let mut mem = Vec::new(); let wr = &mut mem as &mut dyn io::Write; for _ in 0..1000 { - wr.write_all("abc".as_bytes()).unwrap(); + wr.write_all(black_box("abc").as_bytes()).unwrap(); } }); } @@ -29,7 +29,7 @@ fn write_vec_macro1(bh: &mut Bencher) { let mut mem = Vec::new(); let wr = &mut mem as &mut dyn io::Write; for _ in 0..1000 { - write!(wr, "abc").unwrap(); + write!(wr, "{}", black_box("abc")).unwrap(); } }); } @@ -40,7 +40,7 @@ fn write_vec_macro2(bh: &mut Bencher) { let mut mem = Vec::new(); let wr = &mut mem as &mut dyn io::Write; for _ in 0..1000 { - write!(wr, "{}", "abc").unwrap(); + write!(wr, "{}", black_box("abc")).unwrap(); } }); } @@ -51,7 +51,7 @@ fn write_vec_macro_debug(bh: &mut Bencher) { let mut mem = Vec::new(); let wr = &mut mem as &mut dyn io::Write; for _ in 0..1000 { - write!(wr, "{:?}", "☃").unwrap(); + write!(wr, "{:?}", black_box("☃")).unwrap(); } }); } @@ -61,7 +61,7 @@ fn write_str_value(bh: &mut Bencher) { bh.iter(|| { let mut mem = String::new(); for _ in 0..1000 { - mem.write_str("abc").unwrap(); + mem.write_str(black_box("abc")).unwrap(); } }); } @@ -72,7 +72,7 @@ fn write_str_ref(bh: &mut Bencher) { let mut mem = String::new(); let wr = &mut mem as &mut dyn fmt::Write; for _ in 0..1000 { - wr.write_str("abc").unwrap(); + wr.write_str(black_box("abc")).unwrap(); } }); } @@ -82,7 +82,7 @@ fn write_str_macro1(bh: &mut Bencher) { bh.iter(|| { let mut mem = String::new(); for _ in 0..1000 { - write!(mem, "abc").unwrap(); + write!(mem, "{}", black_box("abc")).unwrap(); } }); } @@ -93,7 +93,7 @@ fn write_str_macro2(bh: &mut Bencher) { let mut mem = String::new(); let wr = &mut mem as &mut dyn fmt::Write; for _ in 0..1000 { - write!(wr, "{}", "abc").unwrap(); + write!(wr, "{}", black_box("abc")).unwrap(); } }); } @@ -104,7 +104,7 @@ fn write_str_macro_debug(bh: &mut Bencher) { let mut mem = String::new(); let wr = &mut mem as &mut dyn fmt::Write; for _ in 0..1000 { - write!(wr, "{:?}", "☃").unwrap(); + write!(wr, "{:?}", black_box("☃")).unwrap(); } }); } @@ -115,7 +115,7 @@ fn write_str_macro_debug_ascii(bh: &mut Bencher) { let mut mem = String::new(); let wr = &mut mem as &mut dyn fmt::Write; for _ in 0..1000 { - write!(wr, "{:?}", "Hello, World!").unwrap(); + write!(wr, "{:?}", black_box("Hello, World!")).unwrap(); } }); } diff --git a/library/core/benches/num/dec2flt/mod.rs b/library/core/benches/num/dec2flt/mod.rs index 305baa68729..fb4a786b27e 100644 --- a/library/core/benches/num/dec2flt/mod.rs +++ b/library/core/benches/num/dec2flt/mod.rs @@ -1,57 +1,57 @@ -use test::Bencher; +use test::{black_box, Bencher}; #[bench] fn bench_0(b: &mut Bencher) { - b.iter(|| "0.0".parse::()); + b.iter(|| black_box("0.0").parse::()); } #[bench] fn bench_42(b: &mut Bencher) { - b.iter(|| "42".parse::()); + b.iter(|| black_box("42").parse::()); } #[bench] fn bench_huge_int(b: &mut Bencher) { // 2^128 - 1 - b.iter(|| "170141183460469231731687303715884105727".parse::()); + b.iter(|| black_box("170141183460469231731687303715884105727").parse::()); } #[bench] fn bench_short_decimal(b: &mut Bencher) { - b.iter(|| "1234.5678".parse::()); + b.iter(|| black_box("1234.5678").parse::()); } #[bench] fn bench_pi_long(b: &mut Bencher) { - b.iter(|| "3.14159265358979323846264338327950288".parse::()); + b.iter(|| black_box("3.14159265358979323846264338327950288").parse::()); } #[bench] fn bench_pi_short(b: &mut Bencher) { - b.iter(|| "3.141592653589793".parse::()) + b.iter(|| black_box("3.141592653589793").parse::()) } #[bench] fn bench_1e150(b: &mut Bencher) { - b.iter(|| "1e150".parse::()); + b.iter(|| black_box("1e150").parse::()); } #[bench] fn bench_long_decimal_and_exp(b: &mut Bencher) { - b.iter(|| "727501488517303786137132964064381141071e-123".parse::()); + b.iter(|| black_box("727501488517303786137132964064381141071e-123").parse::()); } #[bench] fn bench_min_subnormal(b: &mut Bencher) { - b.iter(|| "5e-324".parse::()); + b.iter(|| black_box("5e-324").parse::()); } #[bench] fn bench_min_normal(b: &mut Bencher) { - b.iter(|| "2.2250738585072014e-308".parse::()); + b.iter(|| black_box("2.2250738585072014e-308").parse::()); } #[bench] fn bench_max(b: &mut Bencher) { - b.iter(|| "1.7976931348623157e308".parse::()); + b.iter(|| black_box("1.7976931348623157e308").parse::()); } diff --git a/library/core/benches/num/flt2dec/mod.rs b/library/core/benches/num/flt2dec/mod.rs index 32fd5e626bc..1a330ef5fe5 100644 --- a/library/core/benches/num/flt2dec/mod.rs +++ b/library/core/benches/num/flt2dec/mod.rs @@ -7,7 +7,7 @@ use core::num::flt2dec::MAX_SIG_DIGITS; use core::num::flt2dec::{decode, DecodableFloat, Decoded, FullDecoded}; use std::io::Write; use std::vec::Vec; -use test::Bencher; +use test::{black_box, Bencher}; pub fn decode_finite(v: T) -> Decoded { match decode(v).1 { @@ -22,7 +22,7 @@ fn bench_small_shortest(b: &mut Bencher) { b.iter(|| { buf.clear(); - write!(&mut buf, "{}", 3.1415926f64).unwrap() + write!(black_box(&mut buf), "{}", black_box(3.1415926f64)).unwrap() }); } @@ -32,6 +32,6 @@ fn bench_big_shortest(b: &mut Bencher) { b.iter(|| { buf.clear(); - write!(&mut buf, "{}", f64::MAX).unwrap() + write!(black_box(&mut buf), "{}", black_box(f64::MAX)).unwrap() }); } diff --git a/library/core/benches/num/mod.rs b/library/core/benches/num/mod.rs index 2f9cad2725d..b97014d9bf9 100644 --- a/library/core/benches/num/mod.rs +++ b/library/core/benches/num/mod.rs @@ -3,7 +3,7 @@ mod flt2dec; mod int_log; use std::str::FromStr; -use test::Bencher; +use test::{black_box, Bencher}; const ASCII_NUMBERS: [&str; 19] = [ "0", @@ -36,7 +36,7 @@ macro_rules! from_str_bench { .iter() .cycle() .take(5_000) - .filter_map(|s| <$t>::from_str(s).ok()) + .filter_map(|s| <$t>::from_str(black_box(s)).ok()) .max() }) } @@ -52,7 +52,7 @@ macro_rules! from_str_radix_bench { .iter() .cycle() .take(5_000) - .filter_map(|s| <$t>::from_str_radix(s, $radix).ok()) + .filter_map(|s| <$t>::from_str_radix(black_box(s), $radix).ok()) .max() }) }