Extract computation of relative speed
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent 06a2885 · Browse files · 3 files·+87 added−27 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+87 added−27 removed
- other:+87 added−27 removed
Provenance
- Commit
e09707c- Tree
b8ab283- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
06a2885
3 files changed+87 added−27 removed
modified:src/hyperfine/internal.rs+80 added-25 removed
1 use colored::*;2 use indicatif::{ProgressBar, ProgressStyle};3 4 use crate::hyperfine::types::{BenchmarkResult, OutputStyleOption};5-use crate::hyperfine::units::Second;5+use crate::hyperfine::units::{Scalar, Second};6 7 use std::cmp::Ordering;8 use std::iter::Iterator;9 35 unmodified lines45 .min_by(|a, b| a.partial_cmp(b).unwrap())46 .unwrap()47 }48 49+pub struct BenchmarkResultWithRelativeSpeed<'a> {50+ result: &'a BenchmarkResult,51+ relative_speed: Scalar,52+ relative_speed_stddev: Scalar,53+}54+55+fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {56+ l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)57+}58+59+pub fn compute_relative_speed<'a>(60+ results: &'a Vec<BenchmarkResult>,61+) -> Vec<BenchmarkResultWithRelativeSpeed<'a>> {62+ let fastest: &BenchmarkResult = results63+ .iter()64+ .min_by(|&l, &r| compare_mean_time(l, r))65+ .expect("at least one benchmark result");66+67+ results68+ .iter()69+ .map(|result| {70+ let ratio = result.mean / fastest.mean;71+72+ // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas73+ // Covariance asssumed to be 0, i.e. variables are assumed to be independent74+ let ratio_stddev = ratio75+ * ((result.stddev / result.mean).powi(2)76+ + (fastest.stddev / fastest.mean).powi(2))77+ .sqrt();78+79+ BenchmarkResultWithRelativeSpeed {80+ result,81+ relative_speed: ratio,82+ relative_speed_stddev: ratio_stddev,83+ }84+ })85+ .collect()86+}87+88 pub fn write_benchmark_comparison(results: &Vec<BenchmarkResult>) {89 if results.len() < 2 {90 return;91 }92 54- // Show which was faster, maybe expand to table later?55- let mut fastest_item: &BenchmarkResult = &results[0];56- let mut longer_items: Vec<&BenchmarkResult> = Vec::new();93+ let mut annotated_results = compute_relative_speed(&results);94+ annotated_results.sort_by(|l, r| compare_mean_time(l.result, r.result));95 58- for run in &results[1..] {59- if let Some(Ordering::Less) = fastest_item.mean.partial_cmp(&run.mean) {60- longer_items.push(run);61- } else {62- longer_items.push(fastest_item);63- fastest_item = run;64- }65- }96+ let fastest = &annotated_results[0];97+ let others = &annotated_results[1..];98 99 println!("{}", "Summary".bold());68- println!(" '{}' ran", fastest_item.command.cyan());69- longer_items.sort_by(|l, r| l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal));100+ println!(" '{}' ran", fastest.result.command.cyan());101 71- for item in longer_items {72- let ratio = item.mean / fastest_item.mean;73- // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas74- // Covariance asssumed to be 0, i.e. variables are assumed to be independent75- let ratio_stddev = ratio76- * ((item.stddev / item.mean).powi(2)77- + (fastest_item.stddev / fastest_item.mean).powi(2))78- .sqrt();102+ for item in others {103 println!(104 "{} ± {} times faster than '{}'",81- format!("{:8.2}", ratio).bold().green(),82- format!("{:.2}", ratio_stddev).green(),83- &item.command.magenta()105+ format!("{:8.2}", item.relative_speed).bold().green(),106+ format!("{:.2}", item.relative_speed_stddev).green(),107+ &item.result.command.magenta()108 );109 }110 }111 4 unmodified lines116 assert_eq!(-1.0, max(&[-2.0, -1.0]));117 assert_eq!(1.0, max(&[-1.0, 1.0]));118 assert_eq!(1.0, max(&[-1.0, 1.0, 0.0]));119 }120+121+#[test]122+fn test_compute_relative_speed() {123+ use approx::assert_relative_eq;124+125+ let create_result = |name: &str, mean| {126+ BenchmarkResult {127+ command: name.into(),128+ mean: mean,129+ stddev: 1.0,130+ user: mean,131+ system: 0.0,132+ min: mean,133+ max: mean,134+ times: None,135+ parameter: None,136+ }137+ };138+139+ let results = vec![140+ create_result("cmd1", 3.0),141+ create_result("cmd2", 2.0),142+ create_result("cmd3", 5.0),143+ ];144+145+ let annotated_results = compute_relative_speed(&results);146+147+ assert_relative_eq!(1.5, annotated_results[0].relative_speed);148+ assert_relative_eq!(1.0, annotated_results[1].relative_speed);149+ assert_relative_eq!(2.5, annotated_results[2].relative_speed);150+}modified:src/hyperfine/units.rs+3 added-1 removed
1 /// This module contains common units.2 3+pub type Scalar = f64;4+5 /// Type alias for unit of time4-pub type Second = f64;6+pub type Second = Scalar;7 8 /// Supported time units9 #[derive(Debug, Clone, Copy, PartialEq)]10 pub enum Unit {35 unmodified linesmodified:src/main.rs+4 added-1 removed
25 unmodified lines26 std::process::exit(1);27 }28 29 /// Runs the benchmark for the given commands30-fn run(commands: &Vec<Command<'_>>, options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> {30+fn run(31+ commands: &Vec<Command<'_>>,32+ options: &HyperfineOptions,33+) -> io::Result<Vec<BenchmarkResult>> {34 let shell_spawning_time =35 mean_shell_spawning_time(&options.shell, &options.output_style, options.show_output)?;36 37 let mut timing_results = vec![];191 unmodified lines