Skip to content
Myr

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

3 files+87 added−27 removed

modified:src/hyperfine/internal.rs+80 added-25 removed
1use colored::*;2use indicatif::{ProgressBar, ProgressStyle};34use crate::hyperfine::types::{BenchmarkResult, OutputStyleOption};5use crate::hyperfine::units::Second;5use crate::hyperfine::units::{Scalar, Second};67use std::cmp::Ordering;8use std::iter::Iterator;9
35 unmodified lines
45 .min_by(|a, b| a.partial_cmp(b).unwrap())46 .unwrap()47}4849pub struct BenchmarkResultWithRelativeSpeed<'a> {50 result: &'a BenchmarkResult,51 relative_speed: Scalar,52 relative_speed_stddev: Scalar,53}5455fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {56 l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)57}5859pub 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");6667 results68 .iter()69 .map(|result| {70 let ratio = result.mean / fastest.mean;7172 // 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();7879 BenchmarkResultWithRelativeSpeed {80 result,81 relative_speed: ratio,82 relative_speed_stddev: ratio_stddev,83 }84 })85 .collect()86}8788pub fn write_benchmark_comparison(results: &Vec<BenchmarkResult>) {89 if results.len() < 2 {90 return;91 }9254 // 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));9558 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..];9899 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());10171 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 lines
116 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}120121#[test]122fn test_compute_relative_speed() {123 use approx::assert_relative_eq;124125 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 };138139 let results = vec![140 create_result("cmd1", 3.0),141 create_result("cmd2", 2.0),142 create_result("cmd3", 5.0),143 ];144145 let annotated_results = compute_relative_speed(&results);146147 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.23pub type Scalar = f64;45/// Type alias for unit of time4pub type Second = f64;6pub type Second = Scalar;78/// Supported time units9#[derive(Debug, Clone, Copy, PartialEq)]10pub enum Unit {
35 unmodified lines
modified:src/main.rs+4 added-1 removed
25 unmodified lines
26 std::process::exit(1);27}2829/// Runs the benchmark for the given commands30fn run(commands: &Vec<Command<'_>>, options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> {30fn 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)?;3637 let mut timing_results = vec![];
191 unmodified lines