Add min and max, closes #3
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 05e5899 · Browse files · 3 files·+41 added−13 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+41 added−13 removed
- other:+41 added−13 removed
Provenance
- Commit
0047938- Tree
860c3c4- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
05e5899
3 files changed+41 added−13 removed
modified:src/hyperfine/benchmark.rs+22 added-10 removed
1 use std::io;2 use std::process::{Command, Stdio};3 use std::time::Instant;4 5-use ansi_term::Colour::{Cyan, Green, White, Yellow};5+use ansi_term::Colour::{Cyan, Green, White, Yellow, Purple};6 use statistical::{mean, standard_deviation};7 8-use hyperfine::internal::{get_progress_bar, HyperfineOptions, Second, Warnings, MIN_EXECUTION_TIME};8+use hyperfine::internal::{get_progress_bar, max, min, HyperfineOptions, Second, Warnings,9+ MIN_EXECUTION_TIME};10 use hyperfine::format::{format_duration, format_duration_unit, Unit};11 12 /// Results from timing a single shell command13 pub struct TimingResult {158 unmodified lines172 }173 bar.finish_and_clear();174 175 // Compute statistical quantities176+ let t_mean = mean(&execution_times);177+ let t_stddev = standard_deviation(&execution_times, Some(t_mean));178+ let t_min = min(&execution_times);179+ let t_max = max(&execution_times);180 176- // Corrected execution times177- let mean = mean(&execution_times);178- let stddev = standard_deviation(&execution_times, Some(mean));179-181 // Formatting and console output181- let (mean_str, unit_mean) = format_duration_unit(mean, Unit::Auto);182- let stddev_str = format_duration(stddev, unit_mean);183- let time_fmt = format!("{} ± {}", mean_str, stddev_str);182+ let (mean_str, unit_mean) = format_duration_unit(t_mean, Unit::Auto);183+ let stddev_str = format_duration(t_stddev, unit_mean);184+ let min_str = format_duration(t_min, unit_mean);185+ let max_str = format_duration(t_max, unit_mean);186 185- println!(" Time: {}", Green.paint(time_fmt));187+ let mean_stddev = format!("{} ± {}", Green.bold().paint(mean_str), Green.paint(stddev_str));188+ println!(189+ " Time: {:<40} mean ± stddev",190+ mean_stddev191+ );192 193+ let min_max = format!("{} … {}", Purple.paint(min_str), Purple.paint(max_str));194+ println!(195+ " Range: {:<38} min … max",196+ min_max197+ );198+199 // Warnings200 let mut warnings = vec![];201 202 // Check execution time20 unmodified linesmodified:src/hyperfine/format.rs+3 added-3 removed
1 use hyperfine::internal::Second;2 3-#[derive(PartialEq)]3+#[derive(Clone, Copy, PartialEq)]4 pub enum Unit {5 Auto,6 Second,7 MilliSecond,6 unmodified lines14 }15 16 pub fn format_duration_unit(duration: Second, unit: Unit) -> (String, Unit) {17 if (duration < 1.0 && unit == Unit::Auto) || unit == Unit::MilliSecond {18- (format!("{:.1} ms", duration * 1e3), Unit::MilliSecond)18+ (format!("{:5.1} ms", duration * 1e3), Unit::MilliSecond)19 } else {20- (format!("{:.3} s", duration), Unit::Second)20+ (format!("{:5.3} s", duration), Unit::Second)21 }22 }modified:src/hyperfine/internal.rs+16 added
64 unmodified lines65 &Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),66 }67 }68 }69+70+/// A max function for f64's without NaNs71+pub fn max(vals: &[f64]) -> f64 {72+ vals.iter()73+ .max_by(|a, b| a.partial_cmp(b).unwrap())74+ .unwrap()75+ .clone()76+}77+78+/// A min function for f64's without NaNs79+pub fn min(vals: &[f64]) -> f64 {80+ vals.iter()81+ .min_by(|a, b| a.partial_cmp(b).unwrap())82+ .unwrap()83+ .clone()84+}