Skip to content
Myr

Move relative-speed computation to separate module

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent d5a2590 · Browse files · 4 files+100 added−100 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+100 added−100 removed

  • other:+100 added−100 removed

4 files+100 added−100 removed

modified:src/export/markdown.rs+2 added-2 removed
1 unmodified line
23use super::Exporter;4use crate::benchmark_result::BenchmarkResult;5use crate::format::format_duration_value;6use crate::internal::{compute_relative_speed, BenchmarkResultWithRelativeSpeed};6use crate::relative_speed::{self, BenchmarkResultWithRelativeSpeed};7use crate::units::Unit;89#[derive(Default)]10pub struct MarkdownExporter {}
10 unmodified lines
21 // Default to `Second`.22 Unit::Second23 };2425 if let Some(annotated_results) = compute_relative_speed(results) {25 if let Some(annotated_results) = relative_speed::compute(results) {26 let mut destination = start_table(unit);2728 for result in annotated_results {29 add_table_row(&mut destination, &result, unit);
263 unmodified lines
modified:src/internal.rs+1 added-96 removed
1use std::cmp::Ordering;1use std::iter::Iterator;24use crate::benchmark_result::BenchmarkResult;5use crate::units::{Scalar, Second};3use crate::units::Second;45/// Threshold for warning about fast execution time6pub const MIN_EXECUTION_TIME: Second = 5e-3;7
12 unmodified lines
20 .min_by(|a, b| a.partial_cmp(b).unwrap())21 .unwrap()22}2326#[derive(Debug)]27pub struct BenchmarkResultWithRelativeSpeed<'a> {28 pub result: &'a BenchmarkResult,29 pub relative_speed: Scalar,30 pub relative_speed_stddev: Scalar,31 pub is_fastest: bool,32}3334pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {35 l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)36}3738pub fn compute_relative_speed(39 results: &[BenchmarkResult],40) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {41 let fastest: &BenchmarkResult = results42 .iter()43 .min_by(|&l, &r| compare_mean_time(l, r))44 .expect("at least one benchmark result");4546 if fastest.mean == 0.0 {47 return None;48 }4950 Some(51 results52 .iter()53 .map(|result| {54 let ratio = result.mean / fastest.mean;5556 // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas57 // Covariance asssumed to be 0, i.e. variables are assumed to be independent58 let ratio_stddev = ratio59 * ((result.stddev / result.mean).powi(2)60 + (fastest.stddev / fastest.mean).powi(2))61 .sqrt();6263 BenchmarkResultWithRelativeSpeed {64 result,65 relative_speed: ratio,66 relative_speed_stddev: ratio_stddev,67 is_fastest: result == fastest,68 }69 })70 .collect(),71 )72}7324#[test]25fn test_max() {26 let assert_float_eq = |a: f64, b: f64| {27 assert!((a - b).abs() < f64::EPSILON);
4 unmodified lines
32 assert_float_eq(-1.0, max(&[-2.0, -1.0]));33 assert_float_eq(1.0, max(&[-1.0, 1.0]));34 assert_float_eq(1.0, max(&[-1.0, 1.0, 0.0]));35}8687#[cfg(test)]88fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {89 use std::collections::BTreeMap;9091 BenchmarkResult {92 command: name.into(),93 mean,94 stddev: 1.0,95 median: mean,96 user: mean,97 system: 0.0,98 min: mean,99 max: mean,100 times: None,101 exit_codes: Vec::new(),102 parameters: BTreeMap::new(),103 }104}105106#[test]107fn test_compute_relative_speed() {108 use approx::assert_relative_eq;109110 let results = vec![111 create_result("cmd1", 3.0),112 create_result("cmd2", 2.0),113 create_result("cmd3", 5.0),114 ];115116 let annotated_results = compute_relative_speed(&results).unwrap();117118 assert_relative_eq!(1.5, annotated_results[0].relative_speed);119 assert_relative_eq!(1.0, annotated_results[1].relative_speed);120 assert_relative_eq!(2.5, annotated_results[2].relative_speed);121}122123#[test]124fn test_compute_relative_speed_for_zero_times() {125 let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];126127 let annotated_results = compute_relative_speed(&results);128129 assert!(annotated_results.is_none());130}
modified:src/main.rs+3 added-2 removed
17 unmodified lines
18pub mod options;19pub mod outlier_detection;20pub mod parameter_range;21pub mod progress_bar;22pub mod relative_speed;23pub mod shell;24pub mod timer;25pub mod tokenize;26pub mod types;
22 unmodified lines
49 if results.len() < 2 {50 return;51 }5252 if let Some(mut annotated_results) = internal::compute_relative_speed(results) {53 annotated_results.sort_by(|l, r| internal::compare_mean_time(l.result, r.result));53 if let Some(mut annotated_results) = relative_speed::compute(results) {54 annotated_results.sort_by(|l, r| relative_speed::compare_mean_time(l.result, r.result));5556 let fastest = &annotated_results[0];57 let others = &annotated_results[1..];58
402 unmodified lines
added:src/relative_speed.rs+94 added
1use std::cmp::Ordering;23use crate::{benchmark_result::BenchmarkResult, units::Scalar};45#[derive(Debug)]6pub struct BenchmarkResultWithRelativeSpeed<'a> {7 pub result: &'a BenchmarkResult,8 pub relative_speed: Scalar,9 pub relative_speed_stddev: Scalar,10 pub is_fastest: bool,11}1213pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {14 l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)15}1617pub fn compute(results: &[BenchmarkResult]) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {18 let fastest: &BenchmarkResult = results19 .iter()20 .min_by(|&l, &r| compare_mean_time(l, r))21 .expect("at least one benchmark result");2223 if fastest.mean == 0.0 {24 return None;25 }2627 Some(28 results29 .iter()30 .map(|result| {31 let ratio = result.mean / fastest.mean;3233 // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas34 // Covariance asssumed to be 0, i.e. variables are assumed to be independent35 let ratio_stddev = ratio36 * ((result.stddev / result.mean).powi(2)37 + (fastest.stddev / fastest.mean).powi(2))38 .sqrt();3940 BenchmarkResultWithRelativeSpeed {41 result,42 relative_speed: ratio,43 relative_speed_stddev: ratio_stddev,44 is_fastest: result == fastest,45 }46 })47 .collect(),48 )49}5051#[cfg(test)]52fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {53 use std::collections::BTreeMap;5455 BenchmarkResult {56 command: name.into(),57 mean,58 stddev: 1.0,59 median: mean,60 user: mean,61 system: 0.0,62 min: mean,63 max: mean,64 times: None,65 exit_codes: Vec::new(),66 parameters: BTreeMap::new(),67 }68}6970#[test]71fn test_compute_relative_speed() {72 use approx::assert_relative_eq;7374 let results = vec![75 create_result("cmd1", 3.0),76 create_result("cmd2", 2.0),77 create_result("cmd3", 5.0),78 ];7980 let annotated_results = compute(&results).unwrap();8182 assert_relative_eq!(1.5, annotated_results[0].relative_speed);83 assert_relative_eq!(1.0, annotated_results[1].relative_speed);84 assert_relative_eq!(2.5, annotated_results[2].relative_speed);85}8687#[test]88fn test_compute_relative_speed_for_zero_times() {89 let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];9091 let annotated_results = compute(&results);9293 assert!(annotated_results.is_none());94}