Use 'enum' to represent numeric/textual parameter values
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent 4a5d08d · Browse files · 4 files·+35 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+35 added−15 removed
- other:+35 added−15 removed
Provenance
- Commit
d7dc232- Tree
b477c28- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
4a5d08d
4 files changed+35 added−15 removed
modified:src/hyperfine/benchmark.rs+5 added-8 removed
144 unmodified lines145 progress_bar.as_ref().map(|bar| bar.inc(1));146 }147 148 progress_bar.as_ref().map(|bar| bar.finish_and_clear());149- 149+150 Ok(TimingResult {151 time_real: mean(×_real),152 time_user: mean(×_user),153 time_system: mean(×_system),70 unmodified lines224 } else {225 &values[num]226 };227 match cmd.get_parameter() {228- Some((param, value)) => Command::new_parametrized(preparation_command, param, value.clone()),228+ Some((param, value)) => Command::new_parametrized(preparation_command, param, *value),229 None => Command::new(preparation_command),230 }231 });232 18 unmodified lines251 options.failure_action,252 None,253 )?;254 progress_bar.as_ref().map(|bar| bar.inc(1));255- 255 }256 progress_bar.as_ref().map(|bar| bar.finish_and_clear());258- 257 }258 259 // Set up progress bar (and spinner for initial measurement)260 let progress_bar = if options.output_style != OutputStyleOption::Disabled {44 unmodified lines305 306 // Re-configure the progress bar307 progress_bar.as_ref().map(|bar| bar.set_length(count));308 progress_bar.as_ref().map(|bar| bar.inc(1));311- 309+310 // Gather statistics311 for _ in 0..count_remaining {312 run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;313 22 unmodified lines336 }337 338 progress_bar.as_ref().map(|bar| bar.finish_and_clear());339 342-340 // Compute statistical quantities341 let t_num = times_real.len();342 let t_mean = mean(×_real);343 let t_stddev = standard_deviation(×_real, Some(t_mean));73 unmodified lines417 options418 .cleanup_command419 .as_ref()420 .map(|cleanup_command| match cmd.get_parameter() {424- Some((param, value)) => Command::new_parametrized(cleanup_command, param, value.clone()),421+ Some((param, value)) => Command::new_parametrized(cleanup_command, param, *value),422 None => Command::new(cleanup_command),423 });424 run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?;425 6 unmodified lines432 system_mean,433 t_min,434 t_max,435 times_real,439- cmd.get_parameter().as_ref().map(|p| p.1.clone()),436+ cmd.get_parameter().map(|p| p.1.to_string()),437 ))438 }modified:src/hyperfine/parameter_range.rs+6 added-2 removed
1 use crate::hyperfine::error::ParameterScanError;2-use crate::hyperfine::types::{Command, NumericType};2+use crate::hyperfine::types::{Command, NumericType, ParameterValue};3 use clap::Values;4 use rust_decimal::Decimal;5 use std::ops::{Add, AddAssign, Div, Sub};6 use std::str::FromStr;84 unmodified lines91 let mut commands = vec![];92 93 for value in param_range {94 for cmd in &command_strings {95- commands.push(Command::new_parametrized(cmd, param_name, value.into().to_string()));95+ commands.push(Command::new_parametrized(96+ cmd,97+ param_name,98+ ParameterValue::Numeric(value.into()),99+ ));100 }101 }102 Ok(commands)103 }80 unmodified linesmodified:src/hyperfine/types.rs+18 added-3 removed
37 unmodified lines38 NumericType::Decimal(self)39 }40 }41 42+#[derive(Debug, Clone, Copy)]43+pub enum ParameterValue<'a> {44+ Text(&'a str),45+ Numeric(NumericType),46+}47+48+impl<'a> ToString for ParameterValue<'a> {49+ fn to_string(&self) -> String {50+ match self {51+ ParameterValue::Text(value) => value.to_string(),52+ ParameterValue::Numeric(value) => value.to_string(),53+ }54+ }55+}56+57 /// A command that should be benchmarked.58 #[derive(Debug, Clone)]59 pub struct Command<'a> {60 /// The command that should be executed (without parameter substitution)61 expression: &'a str,62 63 /// A possible parameter value.49- parameter: Option<(&'a str, String)>,64+ parameter: Option<(&'a str, ParameterValue<'a>)>,65 }66 67 impl<'a> Command<'a> {68 pub fn new(expression: &'a str) -> Command<'a> {5 unmodified lines74 75 pub fn new_parametrized(76 expression: &'a str,77 parameter: &'a str,63- value: String,78+ value: ParameterValue<'a>,79 ) -> Command<'a> {80 Command {81 expression,82 parameter: Some((parameter, value)),9 unmodified lines92 None => self.expression.into(),93 }94 }95 81- pub fn get_parameter(&self) -> &Option<(&'a str, String)> {96+ pub fn get_parameter(&self) -> &Option<(&'a str, ParameterValue<'a>)> {97 &self.parameter98 }99 }100 164 unmodified linesmodified:src/main.rs+6 added-2 removed
14 unmodified lines15 use crate::hyperfine::export::{ExportManager, ExportType};16 use crate::hyperfine::internal::write_benchmark_comparison;17 use crate::hyperfine::parameter_range::get_parameterized_commands;18 use crate::hyperfine::types::{19- BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,19+ BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, ParameterValue,20 };21 use crate::hyperfine::units::Unit;22 23 /// Print error message to stderr and terminate194 unmodified lines218 let mut commands = Vec::with_capacity(param_list.len() * command_list.len());219 220 for value in param_list {221 for cmd in &command_list {222- commands.push(Command::new_parametrized(cmd, param_name, value.to_string()));222+ commands.push(Command::new_parametrized(223+ cmd,224+ param_name,225+ ParameterValue::Text(value),226+ ));227 }228 }229 230 commands4 unmodified lines