From d5a2590b98b6e9f0f10d5d3ce00af006771f51f6 Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 24 Aug 2021 22:06:49 +0200 Subject: [PATCH] Move benchmark comparison to main.rs --- src/internal.rs | 38 +------------------------------------- src/main.rs | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/internal.rs b/src/internal.rs index 17ced3a..1367f5a 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -1,8 +1,6 @@ use std::cmp::Ordering; use std::iter::Iterator; -use colored::*; - use crate::benchmark_result::BenchmarkResult; use crate::units::{Scalar, Second}; @@ -33,7 +31,7 @@ pub struct BenchmarkResultWithRelativeSpeed<'a> { pub is_fastest: bool, } -fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering { +pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering { l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal) } @@ -73,40 +71,6 @@ pub fn compute_relative_speed( ) } -pub fn write_benchmark_comparison(results: &[BenchmarkResult]) { - if results.len() < 2 { - return; - } - - if let Some(mut annotated_results) = compute_relative_speed(results) { - annotated_results.sort_by(|l, r| compare_mean_time(l.result, r.result)); - - let fastest = &annotated_results[0]; - let others = &annotated_results[1..]; - - println!("{}", "Summary".bold()); - println!(" '{}' ran", fastest.result.command.cyan()); - - for item in others { - println!( - "{} ± {} times faster than '{}'", - format!("{:8.2}", item.relative_speed).bold().green(), - format!("{:.2}", item.relative_speed_stddev).green(), - &item.result.command.magenta() - ); - } - } else { - eprintln!( - "{}: The benchmark comparison could not be computed as some benchmark times are zero. \ - This could be caused by background interference during the initial calibration phase \ - of hyperfine, in combination with very fast commands (faster than a few milliseconds). \ - Try to re-run the benchmark on a quiet system. If it does not help, you command is \ - most likely too fast to be accurately benchmarked by hyperfine.", - "Note".bold().red() - ); - } -} - #[test] fn test_max() { let assert_float_eq = |a: f64, b: f64| { diff --git a/src/main.rs b/src/main.rs index d4f1cd3..f1a85f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,10 +28,10 @@ pub mod warnings; use app::get_arg_matches; use benchmark::{mean_shell_spawning_time, run_benchmark}; +use benchmark_result::BenchmarkResult; use command::Command; use error::OptionsError; use export::{ExportManager, ExportType}; -use internal::write_benchmark_comparison; use options::{CmdFailureAction, HyperfineOptions, OutputStyleOption}; use parameter_range::get_parameterized_commands; use tokenize::tokenize; @@ -44,6 +44,40 @@ pub fn error(message: &str) -> ! { std::process::exit(1); } +pub fn write_benchmark_comparison(results: &[BenchmarkResult]) { + if results.len() < 2 { + return; + } + + if let Some(mut annotated_results) = internal::compute_relative_speed(results) { + annotated_results.sort_by(|l, r| internal::compare_mean_time(l.result, r.result)); + + let fastest = &annotated_results[0]; + let others = &annotated_results[1..]; + + println!("{}", "Summary".bold()); + println!(" '{}' ran", fastest.result.command.cyan()); + + for item in others { + println!( + "{} ± {} times faster than '{}'", + format!("{:8.2}", item.relative_speed).bold().green(), + format!("{:.2}", item.relative_speed_stddev).green(), + &item.result.command.magenta() + ); + } + } else { + eprintln!( + "{}: The benchmark comparison could not be computed as some benchmark times are zero. \ + This could be caused by background interference during the initial calibration phase \ + of hyperfine, in combination with very fast commands (faster than a few milliseconds). \ + Try to re-run the benchmark on a quiet system. If it does not help, you command is \ + most likely too fast to be accurately benchmarked by hyperfine.", + "Note".bold().red() + ); + } +} + /// Runs the benchmark for the given commands fn run( commands: &[Command<'_>],