diff --git a/src/hyperfine/benchmark.rs b/src/hyperfine/benchmark.rs index 181609a..926b41e 100644 --- a/src/hyperfine/benchmark.rs +++ b/src/hyperfine/benchmark.rs @@ -146,7 +146,7 @@ pub fn mean_shell_spawning_time( } progress_bar.as_ref().map(|bar| bar.finish_and_clear()); - + Ok(TimingResult { time_real: mean(×_real), time_user: mean(×_user), @@ -225,7 +225,7 @@ pub fn run_benchmark( &values[num] }; match cmd.get_parameter() { - Some((param, value)) => Command::new_parametrized(preparation_command, param, value.clone()), + Some((param, value)) => Command::new_parametrized(preparation_command, param, *value), None => Command::new(preparation_command), } }); @@ -252,10 +252,8 @@ pub fn run_benchmark( None, )?; progress_bar.as_ref().map(|bar| bar.inc(1)); - } progress_bar.as_ref().map(|bar| bar.finish_and_clear()); - } // Set up progress bar (and spinner for initial measurement) @@ -308,7 +306,7 @@ pub fn run_benchmark( // Re-configure the progress bar progress_bar.as_ref().map(|bar| bar.set_length(count)); progress_bar.as_ref().map(|bar| bar.inc(1)); - + // Gather statistics for _ in 0..count_remaining { run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?; @@ -339,7 +337,6 @@ pub fn run_benchmark( progress_bar.as_ref().map(|bar| bar.finish_and_clear()); - // Compute statistical quantities let t_num = times_real.len(); let t_mean = mean(×_real); @@ -421,7 +418,7 @@ pub fn run_benchmark( .cleanup_command .as_ref() .map(|cleanup_command| match cmd.get_parameter() { - Some((param, value)) => Command::new_parametrized(cleanup_command, param, value.clone()), + Some((param, value)) => Command::new_parametrized(cleanup_command, param, *value), None => Command::new(cleanup_command), }); run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?; @@ -436,6 +433,6 @@ pub fn run_benchmark( t_min, t_max, times_real, - cmd.get_parameter().as_ref().map(|p| p.1.clone()), + cmd.get_parameter().map(|p| p.1.to_string()), )) } diff --git a/src/hyperfine/parameter_range.rs b/src/hyperfine/parameter_range.rs index c6f4144..5f73580 100644 --- a/src/hyperfine/parameter_range.rs +++ b/src/hyperfine/parameter_range.rs @@ -1,5 +1,5 @@ use crate::hyperfine::error::ParameterScanError; -use crate::hyperfine::types::{Command, NumericType}; +use crate::hyperfine::types::{Command, NumericType, ParameterValue}; use clap::Values; use rust_decimal::Decimal; use std::ops::{Add, AddAssign, Div, Sub}; @@ -92,7 +92,11 @@ fn build_parameterized_commands<'a, T: Numeric>( for value in param_range { for cmd in &command_strings { - commands.push(Command::new_parametrized(cmd, param_name, value.into().to_string())); + commands.push(Command::new_parametrized( + cmd, + param_name, + ParameterValue::Numeric(value.into()), + )); } } Ok(commands) diff --git a/src/hyperfine/types.rs b/src/hyperfine/types.rs index 70e3c38..c34b029 100644 --- a/src/hyperfine/types.rs +++ b/src/hyperfine/types.rs @@ -39,6 +39,21 @@ impl Into for Decimal { } } +#[derive(Debug, Clone, Copy)] +pub enum ParameterValue<'a> { + Text(&'a str), + Numeric(NumericType), +} + +impl<'a> ToString for ParameterValue<'a> { + fn to_string(&self) -> String { + match self { + ParameterValue::Text(value) => value.to_string(), + ParameterValue::Numeric(value) => value.to_string(), + } + } +} + /// A command that should be benchmarked. #[derive(Debug, Clone)] pub struct Command<'a> { @@ -46,7 +61,7 @@ pub struct Command<'a> { expression: &'a str, /// A possible parameter value. - parameter: Option<(&'a str, String)>, + parameter: Option<(&'a str, ParameterValue<'a>)>, } impl<'a> Command<'a> { @@ -60,7 +75,7 @@ impl<'a> Command<'a> { pub fn new_parametrized( expression: &'a str, parameter: &'a str, - value: String, + value: ParameterValue<'a>, ) -> Command<'a> { Command { expression, @@ -78,7 +93,7 @@ impl<'a> Command<'a> { } } - pub fn get_parameter(&self) -> &Option<(&'a str, String)> { + pub fn get_parameter(&self) -> &Option<(&'a str, ParameterValue<'a>)> { &self.parameter } } diff --git a/src/main.rs b/src/main.rs index 95c4220..40f0668 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use crate::hyperfine::export::{ExportManager, ExportType}; use crate::hyperfine::internal::write_benchmark_comparison; use crate::hyperfine::parameter_range::get_parameterized_commands; use crate::hyperfine::types::{ - BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, + BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, ParameterValue, }; use crate::hyperfine::units::Unit; @@ -219,7 +219,11 @@ fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec> { for value in param_list { for cmd in &command_list { - commands.push(Command::new_parametrized(cmd, param_name, value.to_string())); + commands.push(Command::new_parametrized( + cmd, + param_name, + ParameterValue::Text(value), + )); } }