From 295432b40e64655b3f0439c5b85e8308f83f41d1 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Sun, 5 May 2024 22:38:39 +0200 Subject: [PATCH] print walltime benchmarks with subnanosecond precision example results when benchmarking 1-4 serialized ADD instructions ``` running 4 tests test add ... bench: 0.24 ns/iter (+/- 0.00) test add2 ... bench: 0.48 ns/iter (+/- 0.01) test add3 ... bench: 0.72 ns/iter (+/- 0.01) test add4 ... bench: 0.96 ns/iter (+/- 0.01) ``` --- library/test/src/bench.rs | 23 +++++++++++++---------- tests/run-make/libtest-padding/Makefile | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/library/test/src/bench.rs b/library/test/src/bench.rs index 23925e6ea72..9a5dc351f6d 100644 --- a/library/test/src/bench.rs +++ b/library/test/src/bench.rs @@ -68,12 +68,12 @@ pub fn fmt_bench_samples(bs: &BenchSamples) -> String { use std::fmt::Write; let mut output = String::new(); - let median = bs.ns_iter_summ.median as usize; - let deviation = (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as usize; + let median = bs.ns_iter_summ.median; + let deviation = bs.ns_iter_summ.max - bs.ns_iter_summ.min; write!( output, - "{:>11} ns/iter (+/- {})", + "{:>14} ns/iter (+/- {})", fmt_thousands_sep(median, ','), fmt_thousands_sep(deviation, ',') ) @@ -85,24 +85,27 @@ pub fn fmt_bench_samples(bs: &BenchSamples) -> String { } // Format a number with thousands separators -fn fmt_thousands_sep(mut n: usize, sep: char) -> String { +fn fmt_thousands_sep(mut n: f64, sep: char) -> String { use std::fmt::Write; let mut output = String::new(); let mut trailing = false; for &pow in &[9, 6, 3, 0] { let base = 10_usize.pow(pow); - if pow == 0 || trailing || n / base != 0 { - if !trailing { - write!(output, "{}", n / base).unwrap(); - } else { - write!(output, "{:03}", n / base).unwrap(); + if pow == 0 || trailing || n / base as f64 >= 1.0 { + match (pow, trailing) { + // modern CPUs can execute multiple instructions per nanosecond + // e.g. benching an ADD takes about 0.25ns. + (0, true) => write!(output, "{:06.2}", n / base as f64).unwrap(), + (0, false) => write!(output, "{:.2}", n / base as f64).unwrap(), + (_, true) => write!(output, "{:03}", n as usize / base).unwrap(), + _ => write!(output, "{}", n as usize / base).unwrap() } if pow != 0 { output.push(sep); } trailing = true; } - n %= base; + n %= base as f64; } output diff --git a/tests/run-make/libtest-padding/Makefile b/tests/run-make/libtest-padding/Makefile index 42bc1192925..c8e2fc01f67 100644 --- a/tests/run-make/libtest-padding/Makefile +++ b/tests/run-make/libtest-padding/Makefile @@ -2,7 +2,7 @@ # needs-unwind because #[bench] and -Cpanic=abort requires -Zpanic-abort-tests include ../tools.mk -NORMALIZE=sed 's%[0-9,]\{1,\} ns/iter (+/- [0-9,]\{1,\})%?? ns/iter (+/- ??)%' | sed 's%finished in [0-9\.]\{1,\}%finished in ??%' +NORMALIZE=sed 's%[0-9,\.]\{1,\} ns/iter (+/- [0-9,\.]\{1,\})%?? ns/iter (+/- ??)%' | sed 's%finished in [0-9\.]\{1,\}%finished in ??%' all: $(RUSTC) --test tests.rs