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
Provenance
- Commit
76dd9d4- Tree
98202fc- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
d5a2590
4 files changed+100 added−100 removed
modified:src/export/markdown.rs+2 added-2 removed
1 unmodified line2 3 use super::Exporter;4 use crate::benchmark_result::BenchmarkResult;5 use crate::format::format_duration_value;6-use crate::internal::{compute_relative_speed, BenchmarkResultWithRelativeSpeed};6+use crate::relative_speed::{self, BenchmarkResultWithRelativeSpeed};7 use crate::units::Unit;8 9 #[derive(Default)]10 pub struct MarkdownExporter {}10 unmodified lines21 // Default to `Second`.22 Unit::Second23 };24 25- 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);27 28 for result in annotated_results {29 add_table_row(&mut destination, &result, unit);263 unmodified linesmodified:src/internal.rs+1 added-96 removed
1-use std::cmp::Ordering;1 use std::iter::Iterator;2 4-use crate::benchmark_result::BenchmarkResult;5-use crate::units::{Scalar, Second};3+use crate::units::Second;4 5 /// Threshold for warning about fast execution time6 pub const MIN_EXECUTION_TIME: Second = 5e-3;7 12 unmodified lines20 .min_by(|a, b| a.partial_cmp(b).unwrap())21 .unwrap()22 }23 26-#[derive(Debug)]27-pub 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-}33-34-pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {35- l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)36-}37-38-pub 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");45-46- if fastest.mean == 0.0 {47- return None;48- }49-50- Some(51- results52- .iter()53- .map(|result| {54- let ratio = result.mean / fastest.mean;55-56- // 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();62-63- BenchmarkResultWithRelativeSpeed {64- result,65- relative_speed: ratio,66- relative_speed_stddev: ratio_stddev,67- is_fastest: result == fastest,68- }69- })70- .collect(),71- )72-}73-24 #[test]25 fn test_max() {26 let assert_float_eq = |a: f64, b: f64| {27 assert!((a - b).abs() < f64::EPSILON);4 unmodified lines32 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 }86-87-#[cfg(test)]88-fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {89- use std::collections::BTreeMap;90-91- 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-}105-106-#[test]107-fn test_compute_relative_speed() {108- use approx::assert_relative_eq;109-110- let results = vec![111- create_result("cmd1", 3.0),112- create_result("cmd2", 2.0),113- create_result("cmd3", 5.0),114- ];115-116- let annotated_results = compute_relative_speed(&results).unwrap();117-118- 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-}122-123-#[test]124-fn test_compute_relative_speed_for_zero_times() {125- let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];126-127- let annotated_results = compute_relative_speed(&results);128-129- assert!(annotated_results.is_none());130-}modified:src/main.rs+3 added-2 removed
17 unmodified lines18 pub mod options;19 pub mod outlier_detection;20 pub mod parameter_range;21 pub mod progress_bar;22+pub mod relative_speed;23 pub mod shell;24 pub mod timer;25 pub mod tokenize;26 pub mod types;22 unmodified lines49 if results.len() < 2 {50 return;51 }52 52- 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));55 56 let fastest = &annotated_results[0];57 let others = &annotated_results[1..];58 402 unmodified linesadded:src/relative_speed.rs+94 added
1+use std::cmp::Ordering;2+3+use crate::{benchmark_result::BenchmarkResult, units::Scalar};4+5+#[derive(Debug)]6+pub 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+}12+13+pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {14+ l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)15+}16+17+pub 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");22+23+ if fastest.mean == 0.0 {24+ return None;25+ }26+27+ Some(28+ results29+ .iter()30+ .map(|result| {31+ let ratio = result.mean / fastest.mean;32+33+ // 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();39+40+ BenchmarkResultWithRelativeSpeed {41+ result,42+ relative_speed: ratio,43+ relative_speed_stddev: ratio_stddev,44+ is_fastest: result == fastest,45+ }46+ })47+ .collect(),48+ )49+}50+51+#[cfg(test)]52+fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {53+ use std::collections::BTreeMap;54+55+ 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+}69+70+#[test]71+fn test_compute_relative_speed() {72+ use approx::assert_relative_eq;73+74+ let results = vec![75+ create_result("cmd1", 3.0),76+ create_result("cmd2", 2.0),77+ create_result("cmd3", 5.0),78+ ];79+80+ let annotated_results = compute(&results).unwrap();81+82+ 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+}86+87+#[test]88+fn test_compute_relative_speed_for_zero_times() {89+ let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];90+91+ let annotated_results = compute(&results);92+93+ assert!(annotated_results.is_none());94+}