Skip to content
Myr

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

3 files+41 added−13 removed

modified:src/hyperfine/benchmark.rs+22 added-10 removed
1use std::io;2use std::process::{Command, Stdio};3use std::time::Instant;45use ansi_term::Colour::{Cyan, Green, White, Yellow};5use ansi_term::Colour::{Cyan, Green, White, Yellow, Purple};6use statistical::{mean, standard_deviation};78use hyperfine::internal::{get_progress_bar, HyperfineOptions, Second, Warnings, MIN_EXECUTION_TIME};8use hyperfine::internal::{get_progress_bar, max, min, HyperfineOptions, Second, Warnings,9 MIN_EXECUTION_TIME};10use hyperfine::format::{format_duration, format_duration_unit, Unit};1112/// Results from timing a single shell command13pub struct TimingResult {
158 unmodified lines
172 }173 bar.finish_and_clear();174175 // 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);180176 // Corrected execution times177 let mean = mean(&execution_times);178 let stddev = standard_deviation(&execution_times, Some(mean));179181 // 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);186185 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 );192193 let min_max = format!("{} … {}", Purple.paint(min_str), Purple.paint(max_str));194 println!(195 " Range: {:<38} min … max",196 min_max197 );198199 // Warnings200 let mut warnings = vec![];201202 // Check execution time
20 unmodified lines
modified:src/hyperfine/format.rs+3 added-3 removed
1use hyperfine::internal::Second;23#[derive(PartialEq)]3#[derive(Clone, Copy, PartialEq)]4pub enum Unit {5 Auto,6 Second,7 MilliSecond,
6 unmodified lines
14}1516pub 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 lines
65 &Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),66 }67 }68}6970/// A max function for f64's without NaNs71pub fn max(vals: &[f64]) -> f64 {72 vals.iter()73 .max_by(|a, b| a.partial_cmp(b).unwrap())74 .unwrap()75 .clone()76}7778/// A min function for f64's without NaNs79pub fn min(vals: &[f64]) -> f64 {80 vals.iter()81 .min_by(|a, b| a.partial_cmp(b).unwrap())82 .unwrap()83 .clone()84}