Make stddev fields optional, hide "± σ" for 1 run
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 bffd476 · Browse files · 7 files·+106 added−79 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+106 added−79 removed
- other:+106 added−79 removed
Provenance
- Commit
abd0d33- Tree
ffa47d3- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
bffd476
7 files changed+106 added−79 removed
modified:src/export/asciidoc.rs+22 added-22 removed
58 unmodified lines59 | {} ± {}\n\60 | {}…{}\n",61 entry.command.replace("|", "\\|"),62 form(entry.mean).0,63- form(entry.stddev).0,63+ entry.stddev.map(|s| form(s).0).unwrap_or("?".into()),64 form(entry.min).0,65 form(entry.max).066 )67 .into_bytes()15 unmodified lines83 #[test]84 fn test_asciidoc_table_row() {85 use std::collections::BTreeMap;86 let result = BenchmarkResult::new(87- String::from("sleep 1"), // command88- 0.10491992406666667, // mean89- 0.00397851689425097, // stddev90- 0.10491992406666667, // median91- 0.005182013333333333, // user92- 0.0, // system93- 0.1003342584, // min94- 0.10745223440000001, // max87+ String::from("sleep 1"), // command88+ 0.10491992406666667, // mean89+ Some(0.00397851689425097), // stddev90+ 0.10491992406666667, // median91+ 0.005182013333333333, // user92+ 0.0, // system93+ 0.1003342584, // min94+ 0.10745223440000001, // max95 vec![96 // times97 0.1003342584,98 0.10745223440000001,8 unmodified lines107 | {} ± {}\n\108 | {}…{}\n",109 result.command,110 Unit::MilliSecond.format(result.mean),111- Unit::MilliSecond.format(result.stddev),111+ Unit::MilliSecond.format(result.stddev.unwrap()),112 Unit::MilliSecond.format(result.min),113 Unit::MilliSecond.format(result.max)114 )115 .into_bytes();2 unmodified lines118 | {} ± {}\n\119 | {}…{}\n",120 result.command,121 Unit::Second.format(result.mean),122- Unit::Second.format(result.stddev),122+ Unit::Second.format(result.stddev.unwrap()),123 Unit::Second.format(result.min),124 Unit::Second.format(result.max)125 )126 .into_bytes();9 unmodified lines136 #[test]137 fn test_asciidoc_table_row_command_escape() {138 use std::collections::BTreeMap;139 let result = BenchmarkResult::new(140- String::from("sleep 1|"), // command141- 0.10491992406666667, // mean142- 0.00397851689425097, // stddev143- 0.10491992406666667, // median144- 0.005182013333333333, // user145- 0.0, // system146- 0.1003342584, // min147- 0.10745223440000001, // max140+ String::from("sleep 1|"), // command141+ 0.10491992406666667, // mean142+ Some(0.00397851689425097), // stddev143+ 0.10491992406666667, // median144+ 0.005182013333333333, // user145+ 0.0, // system146+ 0.1003342584, // min147+ 0.10745223440000001, // max148 vec![149 // times150 0.1003342584,151 0.10745223440000001,6 unmodified lines158 "| `sleep 1\\|`\n\159 | {} ± {}\n\160 | {}…{}\n",161 Unit::Second.format(result.mean),162- Unit::Second.format(result.stddev),162+ Unit::Second.format(result.stddev.unwrap()),163 Unit::Second.format(result.min),164 Unit::Second.format(result.max)165 )166 .into_bytes();11 unmodified lines178 let results = vec![179 BenchmarkResult::new(180 String::from("FOO=1 BAR=2 command | 1"),181 1.0,182- 2.0,182+ Some(2.0),183 1.0,184 3.0,185 4.0,186 5.0,9 unmodified lines196 ),197 BenchmarkResult::new(198 String::from("FOO=1 BAR=7 command | 2"),199 11.0,200- 12.0,200+ Some(12.0),201 11.0,202 13.0,203 14.0,204 15.0,32 unmodified linesmodified:src/export/csv.rs+9 added-3 removed
31 unmodified lines32 33 for res in results {34 let mut fields = vec![Cow::Borrowed(res.command.as_bytes())];35 for f in &[36- res.mean, res.stddev, res.median, res.user, res.system, res.min, res.max,36+ res.mean,37+ res.stddev.unwrap_or(0.0),38+ res.median,39+ res.user,40+ res.system,41+ res.min,42+ res.max,43 ] {44 fields.push(Cow::Owned(f.to_string().into_bytes()))45 }46 for v in res.parameters.values() {17 unmodified lines64 let results = vec![65 BenchmarkResult::new(66 String::from("FOO=one BAR=two command | 1"),67 1.0,62- 2.0,68+ Some(2.0),69 1.0,70 3.0,71 4.0,72 5.0,9 unmodified lines82 ),83 BenchmarkResult::new(84 String::from("FOO=one BAR=seven command | 2"),85 11.0,80- 12.0,86+ Some(12.0),87 11.0,88 13.0,89 14.0,90 15.0,22 unmodified linesmodified:src/export/markdown.rs+19 added-11 removed
51 unmodified lines52 53 fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResultWithRelativeSpeed, unit: Unit) {54 let result = &entry.result;55 let mean_str = format_duration_value(result.mean, Some(unit)).0;56- let stddev_str = format_duration_value(result.stddev, Some(unit)).0;56+ let stddev_str = if let Some(stddev) = result.stddev {57+ format!(" ± {}", format_duration_value(stddev, Some(unit)).0)58+ } else {59+ "".into()60+ };61 let min_str = format_duration_value(result.min, Some(unit)).0;62 let max_str = format_duration_value(result.max, Some(unit)).0;63 let rel_str = format!("{:.2}", entry.relative_speed);64 let rel_stddev_str = if entry.is_fastest {65 "".into()66 } else {67+ if let Some(stddev) = entry.relative_speed_stddev {63- format!(" ± {:.2}", entry.relative_speed_stddev)68+ format!(" ± {:.2}", stddev)69+ } else {70+ "".into()71+ }72 };73 74 dest.extend(75 format!(68- "| `{command}` | {mean} ± {stddev} | {min} | {max} | {rel}{rel_stddev} |\n",76+ "| `{command}` | {mean}{stddev} | {min} | {max} | {rel}{rel_stddev} |\n",77 command = result.command.replace("|", "\\|"),78 mean = mean_str,79 stddev = stddev_str,80 min = min_str,19 unmodified lines100 let timing_results = vec![101 BenchmarkResult::new(102 String::from("sleep 0.1"),103 0.1057, // mean96- 0.0016, // std dev104+ Some(0.0016), // std dev105 0.1057, // median106 0.0009, // user_mean107 0.0011, // system_mean108 0.1023, // min4 unmodified lines113 ),114 BenchmarkResult::new(115 String::from("sleep 2"),116 2.0050, // mean109- 0.0020, // std dev117+ Some(0.0020), // std dev118 2.0050, // median119 0.0009, // user_mean120 0.0012, // system_mean121 2.0020, // min27 unmodified lines149 let timing_results = vec![150 BenchmarkResult::new(151 String::from("sleep 2"),152 2.0050, // mean145- 0.0020, // std dev153+ Some(0.0020), // std dev154 2.0050, // median155 0.0009, // user_mean156 0.0012, // system_mean157 2.0020, // min4 unmodified lines162 ),163 BenchmarkResult::new(164 String::from("sleep 0.1"),165 0.1057, // mean158- 0.0016, // std dev166+ Some(0.0016), // std dev167 0.1057, // median168 0.0009, // user_mean169 0.0011, // system_mean170 0.1023, // min26 unmodified lines197 let timing_results = vec![198 BenchmarkResult::new(199 String::from("sleep 0.1"),200 0.1057, // mean193- 0.0016, // std dev201+ Some(0.0016), // std dev202 0.1057, // median203 0.0009, // user_mean204 0.0011, // system_mean205 0.1023, // min4 unmodified lines210 ),211 BenchmarkResult::new(212 String::from("sleep 2"),213 2.0050, // mean206- 0.0020, // std dev214+ Some(0.0020), // std dev215 2.0050, // median216 0.0009, // user_mean217 0.0012, // system_mean218 2.0020, // min32 unmodified lines251 let timing_results = vec![252 BenchmarkResult::new(253 String::from("sleep 2"),254 2.0050, // mean247- 0.0020, // std dev255+ Some(0.0020), // std dev256 2.0050, // median257 0.0009, // user_mean258 0.0012, // system_mean259 2.0020, // min4 unmodified lines264 ),265 BenchmarkResult::new(266 String::from("sleep 0.1"),267 0.1057, // mean260- 0.0016, // std dev268+ Some(0.0016), // std dev269 0.1057, // median270 0.0009, // user_mean271 0.0011, // system_mean272 0.1023, // min24 unmodified linesmodified:src/benchmark_result.rs+3 added-3 removed
13 unmodified lines14 15 /// The mean run time16 pub mean: Second,17 18- /// The standard deviation of all run times19- pub stddev: Second,18+ /// The standard deviation of all run times. Not available if only one run has been performed19+ pub stddev: Option<Second>,20 21 /// The median run time22 pub median: Second,23 26 unmodified lines50 #[allow(clippy::too_many_arguments)]51 pub fn new(52 command: String,53 mean: Second,54- stddev: Second,54+ stddev: Option<Second>,55 median: Second,56 user: Second,57 system: Second,58 min: Second,20 unmodified linesmodified:src/benchmark.rs+36 added-32 removed
389 unmodified lines390 391 // Compute statistical quantities392 let t_num = times_real.len();393 let t_mean = mean(×_real);394- let mut t_stddev = 0.0;395- if times_real.len() != 1 {396- t_stddev = standard_deviation(×_real, Some(t_mean));397- }394+ let t_stddev = if times_real.len() > 1 {395+ Some(standard_deviation(×_real, Some(t_mean)))396+ } else {397+ None398+ };399 let t_median = median(×_real);400 let t_min = min(×_real);401 let t_max = max(×_real);402 1 unmodified line404 let system_mean = mean(×_system);405 406 // Formatting and console output407 let (mean_str, time_unit) = format_duration_unit(t_mean, options.time_unit);407- let stddev_str = format_duration(t_stddev, Some(time_unit));408 let min_str = format_duration(t_min, Some(time_unit));409 let max_str = format_duration(t_max, Some(time_unit));410 let num_str = format!("{} runs", t_num);411 412 let user_str = format_duration(user_mean, Some(time_unit));413 let system_str = format_duration(system_mean, Some(time_unit));414 415- if options.output_style != OutputStyleOption::Disabled && times_real.len() == 1 {416- println!(417- " Time ({} ≡): {:>8} {:>8} [User: {}, System: {}]",418- "abs".green().bold(),419- mean_str.green().bold(),420- " ".to_string(), // alignment421- user_str.blue(),422- system_str.blue()423- );424- } else if options.output_style != OutputStyleOption::Disabled {425- println!(426- " Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]",427- "mean".green().bold(),428- "σ".green(),429- mean_str.green().bold(),430- stddev_str.green(),431- user_str.blue(),432- system_str.blue()433- );415+ if options.output_style != OutputStyleOption::Disabled {416+ if times_real.len() == 1 {417+ println!(418+ " Time ({} ≡): {:>8} {:>8} [User: {}, System: {}]",419+ "abs".green().bold(),420+ mean_str.green().bold(),421+ " ".to_string(), // alignment422+ user_str.blue(),423+ system_str.blue()424+ );425+ } else {426+ let stddev_str = format_duration(t_stddev.unwrap(), Some(time_unit));427 428+ println!(429+ " Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]",430+ "mean".green().bold(),431+ "σ".green(),432+ mean_str.green().bold(),433+ stddev_str.green(),434+ user_str.blue(),435+ system_str.blue()436+ );437+435- println!(436- " Range ({} … {}): {:>8} … {:>8} {}",437- "min".cyan(),438- "max".purple(),439- min_str.cyan(),440- max_str.purple(),441- num_str.dimmed()442- );438+ println!(439+ " Range ({} … {}): {:>8} … {:>8} {}",440+ "min".cyan(),441+ "max".purple(),442+ min_str.cyan(),443+ max_str.purple(),444+ num_str.dimmed()445+ );446+ }447 }448 449 // Warnings450 let mut warnings = vec![];54 unmodified linesmodified:src/main.rs+6 added-2 removed
59 unmodified lines60 println!(" '{}' ran", fastest.result.command.cyan());61 62 for item in others {63 println!(64- "{} ± {} times faster than '{}'",64+ "{}{} times faster than '{}'",65 format!("{:8.2}", item.relative_speed).bold().green(),66- format!("{:.2}", item.relative_speed_stddev).green(),66+ if let Some(stddev) = item.relative_speed_stddev {67+ format!(" ± {}", format!("{:.2}", stddev).green())68+ } else {69+ "".into()70+ },71 &item.result.command.magenta()72 );73 }74 } else {380 unmodified linesmodified:src/relative_speed.rs+11 added-6 removed
4 unmodified lines5 #[derive(Debug)]6 pub struct BenchmarkResultWithRelativeSpeed<'a> {7 pub result: &'a BenchmarkResult,8 pub relative_speed: Scalar,9- pub relative_speed_stddev: Scalar,9+ pub relative_speed_stddev: Option<Scalar>,10 pub is_fastest: bool,11 }12 13 pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {17 unmodified lines31 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 = match (result.stddev, fastest.stddev) {36+ (Some(result_stddev), Some(fastest_stddev)) => Some(35- let ratio_stddev = ratio36- * ((result.stddev / result.mean).powi(2)37- + (fastest.stddev / fastest.mean).powi(2))38- .sqrt();37+ ratio38+ * ((result_stddev / result.mean).powi(2)39+ + (fastest_stddev / fastest.mean).powi(2))40+ .sqrt(),41+ ),42+ _ => None,43+ };44 45 BenchmarkResultWithRelativeSpeed {46 result,47 relative_speed: ratio,11 unmodified lines59 60 BenchmarkResult {61 command: name.into(),62 mean,58- stddev: 1.0,63+ stddev: Some(1.0),64 median: mean,65 user: mean,66 system: 0.0,67 min: mean,32 unmodified lines