Skip to content

Fix more benchmark test with black_box #108291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions library/core/benches/fmt.rs
Original file line number Diff line number Diff line change
@@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand All @@ -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();
}
});
}
Expand Down
24 changes: 12 additions & 12 deletions library/core/benches/num/dec2flt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
use test::Bencher;
use test::{black_box, Bencher};

#[bench]
fn bench_0(b: &mut Bencher) {
b.iter(|| "0.0".parse::<f64>());
b.iter(|| black_box("0.0").parse::<f64>());
}

#[bench]
fn bench_42(b: &mut Bencher) {
b.iter(|| "42".parse::<f64>());
b.iter(|| black_box("42").parse::<f64>());
}

#[bench]
fn bench_huge_int(b: &mut Bencher) {
// 2^128 - 1
b.iter(|| "170141183460469231731687303715884105727".parse::<f64>());
b.iter(|| black_box("170141183460469231731687303715884105727").parse::<f64>());
}

#[bench]
fn bench_short_decimal(b: &mut Bencher) {
b.iter(|| "1234.5678".parse::<f64>());
b.iter(|| black_box("1234.5678").parse::<f64>());
}

#[bench]
fn bench_pi_long(b: &mut Bencher) {
b.iter(|| "3.14159265358979323846264338327950288".parse::<f64>());
b.iter(|| black_box("3.14159265358979323846264338327950288").parse::<f64>());
}

#[bench]
fn bench_pi_short(b: &mut Bencher) {
b.iter(|| "3.141592653589793".parse::<f64>())
b.iter(|| black_box("3.141592653589793").parse::<f64>())
}

#[bench]
fn bench_1e150(b: &mut Bencher) {
b.iter(|| "1e150".parse::<f64>());
b.iter(|| black_box("1e150").parse::<f64>());
}

#[bench]
fn bench_long_decimal_and_exp(b: &mut Bencher) {
b.iter(|| "727501488517303786137132964064381141071e-123".parse::<f64>());
b.iter(|| black_box("727501488517303786137132964064381141071e-123").parse::<f64>());
}

#[bench]
fn bench_min_subnormal(b: &mut Bencher) {
b.iter(|| "5e-324".parse::<f64>());
b.iter(|| black_box("5e-324").parse::<f64>());
}

#[bench]
fn bench_min_normal(b: &mut Bencher) {
b.iter(|| "2.2250738585072014e-308".parse::<f64>());
b.iter(|| black_box("2.2250738585072014e-308").parse::<f64>());
}

#[bench]
fn bench_max(b: &mut Bencher) {
b.iter(|| "1.7976931348623157e308".parse::<f64>());
b.iter(|| black_box("1.7976931348623157e308").parse::<f64>());
}
6 changes: 3 additions & 3 deletions library/core/benches/num/flt2dec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: DecodableFloat>(v: T) -> Decoded {
match decode(v).1 {
Expand All @@ -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()
});
}

Expand All @@ -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()
});
}
6 changes: 3 additions & 3 deletions library/core/benches/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()
})
}
Expand All @@ -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()
})
}
Expand Down