Skip to content

Commit

Permalink
Remove str_count as non-reliable benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
bazhenov committed Nov 17, 2024
1 parent 22e1c92 commit a7281f4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 42 deletions.
12 changes: 6 additions & 6 deletions examples/benches/tango-faster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use crate::test_funcs::{factorial, sum};
use std::rc::Rc;
use tango_bench::{benchmark_fn, tango_benchmarks, tango_main, IntoBenchmarks};
use test_funcs::{
create_str_benchmark, sort_unstable, str_count, str_take, vec_benchmarks, IndexedString,
INPUT_TEXT,
create_str_benchmark, sort_unstable, str_take, vec_benchmarks, IndexedString, INPUT_TEXT,
};

mod test_funcs;
Expand All @@ -19,10 +18,11 @@ fn num_benchmarks() -> impl IntoBenchmarks {

fn str_benchmarks() -> impl IntoBenchmarks {
let input = Rc::new(IndexedString::from(INPUT_TEXT));
[
create_str_benchmark("str_length/random", &input, str_count),
create_str_benchmark("str_length/random_limited", &input, |s| str_take(4950, s)),
]
[create_str_benchmark(
"str_length/random_limited",
&input,
|s| str_take(4950, s),
)]
}

tango_benchmarks!(
Expand Down
12 changes: 6 additions & 6 deletions examples/benches/tango-slower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use crate::test_funcs::{factorial, sum};
use std::rc::Rc;
use tango_bench::{benchmark_fn, tango_benchmarks, tango_main, IntoBenchmarks};
use test_funcs::{
create_str_benchmark, sort_stable, str_count_rev, str_take, vec_benchmarks, IndexedString,
INPUT_TEXT,
create_str_benchmark, sort_stable, str_take, vec_benchmarks, IndexedString, INPUT_TEXT,
};

mod test_funcs;
Expand All @@ -19,10 +18,11 @@ fn num_benchmarks() -> impl IntoBenchmarks {

fn str_benchmarks() -> impl IntoBenchmarks {
let input = Rc::new(IndexedString::from(INPUT_TEXT));
[
create_str_benchmark("str_length/random", &input, str_count_rev),
create_str_benchmark("str_length/random_limited", &input, |s| str_take(5000, s)),
]
[create_str_benchmark(
"str_length/random_limited",
&input,
|s| str_take(5000, s),
)]
}

tango_benchmarks!(
Expand Down
24 changes: 0 additions & 24 deletions examples/benches/test_funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,6 @@ pub fn factorial(mut n: usize) -> usize {
result
}

#[cfg_attr(feature = "align", repr(align(32)))]
#[cfg_attr(feature = "align", inline(never))]
#[allow(unused)]
#[allow(clippy::ptr_arg)]
pub fn str_count_rev(s: &str) -> usize {
let mut l = 0;
for _ in s.chars().rev() {
l += 1;
}
l
}

#[cfg_attr(feature = "align", repr(align(32)))]
#[cfg_attr(feature = "align", inline(never))]
#[allow(unused)]
#[allow(clippy::ptr_arg)]
pub fn str_count(s: &str) -> usize {
let mut l = 0;
for _ in s.chars() {
l += 1;
}
l
}

#[cfg_attr(feature = "align", repr(align(32)))]
#[cfg_attr(feature = "align", inline(never))]
#[allow(unused)]
Expand Down
5 changes: 2 additions & 3 deletions tango-bench/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,15 +853,14 @@ mod reporting {

let significant = results.diff_estimate.significant;

let speedup = results.diff_estimate.pct;
let speedup = results.diff_estimate.speedup;
let candidate_faster = diff.mean < 0.;
println!(
"{:50} [ {:>8} ... {:>8} ] {:>+7.2}{}{}",
"{:50} [ {:>8} ... {:>8} ] {:>2.2}{}",
colorize(&results.name, significant, candidate_faster),
HumanTime(base.mean),
colorize(HumanTime(candidate.mean), significant, candidate_faster),
colorize(speedup, significant, candidate_faster),
colorize("%", significant, candidate_faster),
if significant { "*" } else { "" },
)
}
Expand Down
16 changes: 13 additions & 3 deletions tango-bench/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ pub(crate) fn calculate_run_result<N: Into<String>>(
let baseline_summary = Summary::from(&baseline)?;
let candidate_summary = Summary::from(&candidate)?;

let diff_estimate = DiffEstimate::build(&baseline_summary, &diff_summary);
let diff_estimate = DiffEstimate::build(&baseline_summary, &candidate_summary, &diff_summary);

Some(RunResult {
baseline: baseline_summary,
Expand All @@ -526,6 +526,11 @@ pub(crate) struct DiffEstimate {
// Negative value means that candidate is faster than baseline, positive - slower.
pct: f64,

// Speedup of candidate compared to baseline
//
// Values larger than 1 means that candidate is slower than baseline
speedup: f64,

// Is the difference statistically significant
significant: bool,
}
Expand All @@ -538,7 +543,7 @@ impl DiffEstimate {
/// robust to outliers, but it is requiring more iterations.
///
/// It is assumed that baseline and candidate are already normalized by iterations count.
fn build(baseline: &Summary<f64>, diff: &Summary<f64>) -> Self {
fn build(baseline: &Summary<f64>, candidate: &Summary<f64>, diff: &Summary<f64>) -> Self {
let std_dev = diff.variance.sqrt();
let std_err = std_dev / (diff.n as f64).sqrt();
let z_score = diff.mean / std_err;
Expand All @@ -549,8 +554,13 @@ impl DiffEstimate {
&& (diff.mean / baseline.mean).abs() > 0.005
&& diff.mean.abs() >= ActiveTimer::precision() as f64;
let pct = diff.mean / baseline.mean * 100.0;
let speedup = candidate.mean / baseline.mean;

Self { pct, significant }
Self {
pct,
speedup,
significant,
}
}
}

Expand Down

0 comments on commit a7281f4

Please sign in to comment.