Skip to content
Myr

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

4 files+35 added−15 removed

modified:src/hyperfine/benchmark.rs+5 added-8 removed
144 unmodified lines
145 progress_bar.as_ref().map(|bar| bar.inc(1));146 }147148 progress_bar.as_ref().map(|bar| bar.finish_and_clear());149 149150 Ok(TimingResult {151 time_real: mean(&times_real),152 time_user: mean(&times_user),153 time_system: mean(&times_system),
70 unmodified lines
224 } 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 lines
251 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 }258259 // Set up progress bar (and spinner for initial measurement)260 let progress_bar = if options.output_style != OutputStyleOption::Disabled {
44 unmodified lines
305306 // 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 309310 // Gather statistics311 for _ in 0..count_remaining {312 run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;313
22 unmodified lines
336 }337338 progress_bar.as_ref().map(|bar| bar.finish_and_clear());339342340 // Compute statistical quantities341 let t_num = times_real.len();342 let t_mean = mean(&times_real);343 let t_stddev = standard_deviation(&times_real, Some(t_mean));
73 unmodified lines
417 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 lines
432 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
1use crate::hyperfine::error::ParameterScanError;2use crate::hyperfine::types::{Command, NumericType};2use crate::hyperfine::types::{Command, NumericType, ParameterValue};3use clap::Values;4use rust_decimal::Decimal;5use std::ops::{Add, AddAssign, Div, Sub};6use std::str::FromStr;
84 unmodified lines
91 let mut commands = vec![];9293 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 lines
modified:src/hyperfine/types.rs+18 added-3 removed
37 unmodified lines
38 NumericType::Decimal(self)39 }40}4142#[derive(Debug, Clone, Copy)]43pub enum ParameterValue<'a> {44 Text(&'a str),45 Numeric(NumericType),46}4748impl<'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}5657/// A command that should be benchmarked.58#[derive(Debug, Clone)]59pub struct Command<'a> {60 /// The command that should be executed (without parameter substitution)61 expression: &'a str,6263 /// A possible parameter value.49 parameter: Option<(&'a str, String)>,64 parameter: Option<(&'a str, ParameterValue<'a>)>,65}6667impl<'a> Command<'a> {68 pub fn new(expression: &'a str) -> Command<'a> {
5 unmodified lines
7475 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 lines
92 None => self.expression.into(),93 }94 }9581 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 lines
modified:src/main.rs+6 added-2 removed
14 unmodified lines
15use crate::hyperfine::export::{ExportManager, ExportType};16use crate::hyperfine::internal::write_benchmark_comparison;17use crate::hyperfine::parameter_range::get_parameterized_commands;18use crate::hyperfine::types::{19 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,19 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, ParameterValue,20};21use crate::hyperfine::units::Unit;2223/// Print error message to stderr and terminate
194 unmodified lines
218 let mut commands = Vec::with_capacity(param_list.len() * command_list.len());219220 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 }229230 commands
4 unmodified lines