Always compute relative speed comprison for export, closes #642
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 926b8d0 · Browse files · 3 files·+62 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+62 added−38 removed
- other:+62 added−38 removed
Provenance
- Commit
9859eca- Tree
1251bf4- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
926b8d0
3 files changed+62 added−38 removed
modified:src/benchmark/relative_speed.rs+59 added-30 removed
13 unmodified lines14 pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {15 l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)16 }17 18-pub fn compute(results: &[BenchmarkResult]) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {19- let fastest: &BenchmarkResult = results18+fn fastest_of(results: &[BenchmarkResult]) -> &BenchmarkResult {19+ results20 .iter()21 .min_by(|&l, &r| compare_mean_time(l, r))22- .expect("at least one benchmark result");22+ .expect("at least one benchmark result")23+}24 25+fn compute_relative_speeds<'a>(26+ results: &'a [BenchmarkResult],27+ fastest: &'a BenchmarkResult,28+) -> Vec<BenchmarkResultWithRelativeSpeed<'a>> {29+ results30+ .iter()31+ .map(|result| {32+ let is_fastest = result == fastest;33+34+ if result.mean == 0.0 {35+ return BenchmarkResultWithRelativeSpeed {36+ result,37+ relative_speed: if is_fastest { 1.0 } else { f64::INFINITY },38+ relative_speed_stddev: None,39+ is_fastest,40+ };41+ }42+43+ let ratio = result.mean / fastest.mean;44+45+ // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas46+ // Covariance asssumed to be 0, i.e. variables are assumed to be independent47+ let ratio_stddev = match (result.stddev, fastest.stddev) {48+ (Some(result_stddev), Some(fastest_stddev)) => Some(49+ ratio50+ * ((result_stddev / result.mean).powi(2)51+ + (fastest_stddev / fastest.mean).powi(2))52+ .sqrt(),53+ ),54+ _ => None,55+ };56+57+ BenchmarkResultWithRelativeSpeed {58+ result,59+ relative_speed: ratio,60+ relative_speed_stddev: ratio_stddev,61+ is_fastest,62+ }63+ })64+ .collect()65+}66+67+pub fn compute_with_check(68+ results: &[BenchmarkResult],69+) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {70+ let fastest = fastest_of(results);71+72 if fastest.mean == 0.0 {73 return None;74 }75 28- Some(29- results30- .iter()31- .map(|result| {32- let ratio = result.mean / fastest.mean;76+ Some(compute_relative_speeds(results, fastest))77+}78 34- // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas35- // Covariance asssumed to be 0, i.e. variables are assumed to be independent36- let ratio_stddev = match (result.stddev, fastest.stddev) {37- (Some(result_stddev), Some(fastest_stddev)) => Some(38- ratio39- * ((result_stddev / result.mean).powi(2)40- + (fastest_stddev / fastest.mean).powi(2))41- .sqrt(),42- ),43- _ => None,44- };79+/// Same as compute_with_check, potentially resulting in relative speeds of infinity80+pub fn compute(results: &[BenchmarkResult]) -> Vec<BenchmarkResultWithRelativeSpeed> {81+ let fastest = fastest_of(results);82 46- BenchmarkResultWithRelativeSpeed {47- result,48- relative_speed: ratio,49- relative_speed_stddev: ratio_stddev,50- is_fastest: result == fastest,51- }52- })53- .collect(),54- )83+ compute_relative_speeds(results, fastest)84 }85 86 #[cfg(test)]87 fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {23 unmodified lines111 create_result("cmd2", 2.0),112 create_result("cmd3", 5.0),113 ];114 86- let annotated_results = compute(&results).unwrap();115+ let annotated_results = compute_with_check(&results).unwrap();116 117 assert_relative_eq!(1.5, annotated_results[0].relative_speed);118 assert_relative_eq!(1.0, annotated_results[1].relative_speed);119 assert_relative_eq!(2.5, annotated_results[2].relative_speed);2 unmodified lines122 #[test]123 fn test_compute_relative_speed_for_zero_times() {124 let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];125 97- let annotated_results = compute(&results);126+ let annotated_results = compute_with_check(&results);127 128 assert!(annotated_results.is_none());129 }modified:src/benchmark/scheduler.rs+1 added-1 removed
59 unmodified lines60 if self.results.len() < 2 {61 return;62 }63 64- if let Some(mut annotated_results) = relative_speed::compute(&self.results) {64+ if let Some(mut annotated_results) = relative_speed::compute_with_check(&self.results) {65 annotated_results.sort_by(|l, r| relative_speed::compare_mean_time(l.result, r.result));66 67 let fastest = &annotated_results[0];68 let others = &annotated_results[1..];33 unmodified linesmodified:src/export/markup.rs+2 added-7 removed
2 unmodified lines3 use crate::output::format::format_duration_value;4 use crate::util::units::Unit;5 6 use super::Exporter;7-use anyhow::{anyhow, Result};7+use anyhow::Result;8 9 pub enum Alignment {10 Left,11 Right,93 unmodified lines105 impl<T: MarkupExporter> Exporter for T {106 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {107 let unit = unit.unwrap_or_else(|| determine_unit_from_results(results));108 let entries = relative_speed::compute(results);109- if entries.is_none() {110- return Err(anyhow!(111- "Relative speed comparison is not available for markup exporter."112- ));113- }109 115- let table = self.table_results(&entries.unwrap(), unit);110+ let table = self.table_results(&entries, unit);111 Ok(table.as_bytes().to_vec())112 }113 }114 171 unmodified lines