Skip to content
Myr

Correct for shell spawning time, closes #15

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by sharkdp · parent 7e41c2a · Browse files · 1 file+69 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+69 added−17 removed

  • other:+69 added−17 removed

1 file+69 added−17 removed

modified:src/main.rs+69 added-17 removed
85 unmodified lines
86 match self {87 &Warnings::FastExecutionTime => write!(88 f,89 "Command took less than {:.0} ms to complete. \90 The execution time is likely to be dominated by the intermediate shell.",90 Results might be inaccurate due to the intermediate shell call.",91 MIN_EXECUTION_TIME * 1e392 ),93 &Warnings::NonZeroExitCode => write!(f, "Process exited with a non-zero exit code"),94 }95 }96}9798/// Calculate statistical average99fn mean<I>(values: I) -> f64100where101 I: IntoIterator<Item = f64>,102{103 let mut sum: f64 = 0.0;104 let mut len: u64 = 0;105 for v in values {106 sum += v;107 len += 1;108 }109 sum / (len as f64)110}111112/// Run the benchmark for a single shell command99fn run_benchmark(cmd: &str, options: &HyperfineOptions) -> io::Result<()> {113fn run_benchmark(114 cmd: &str,115 shell_spawning_time: Second,116 options: &HyperfineOptions,117) -> io::Result<()> {118 println!("Command: {}", Cyan.paint(cmd));119 println!();120121 let mut results = vec![];
38 unmodified lines
160 }161 bar.finish_and_clear();162163 // Compute statistical quantities146 let get_execution_time = |r: &CmdResult| -> Second { r.execution_time_sec };164 let get_execution_time = |r: &CmdResult| -> Second {165 if r.execution_time_sec < shell_spawning_time {166 0.0167 } else {168 r.execution_time_sec - shell_spawning_time169 }170 };171148 // Iterator over execution times172 // Iterator over (corrected) execution times173 let execution_times = results.iter().map(&get_execution_time);174151 let t_sum: Second = execution_times.clone().sum();152 let t_mean = t_sum / (results.len() as f64);175 let t_mean = mean(execution_times.clone());176 let t2_mean = mean(execution_times.clone().map(|t| t.powi(2)));177154 let t2_sum: Second = execution_times.clone().map(|t| t.powi(2)).sum();155 let t2_mean = t2_sum / (results.len() as f64);156178 let stddev = (t2_mean - t_mean.powi(2)).sqrt();179180 // Formatting and console output181 let time_fmt = format!("{:.3} s ± {:.3} s", t_mean, stddev);
21 unmodified lines
203204 Ok(())205}206207// Measure the average shell spawning time208fn mean_shell_spawning_time() -> io::Result<Second> {209 const COUNT: u64 = 200;210 let bar = get_progress_bar(COUNT, "Measuring shell spawning time");211212 let mut times: Vec<Second> = vec![];213 for _ in 1..COUNT {214 bar.inc(1);215 let res = time_shell_command("")?;216 times.push(res.execution_time_sec);217 }218 bar.finish_and_clear();219220 let mean: f64 = mean(times.iter().cloned()); // TODO: get rid of .cloned()221 Ok(mean)222}223224/// Runs the benchmark for the given commands225fn run(commands: &Vec<&str>, options: &HyperfineOptions) -> io::Result<()> {226 let shell_spawning_time = mean_shell_spawning_time()?;227228 println!(229 "Mean shell spawning time: {:.3} ms",230 1e3 * shell_spawning_time231 );232233 // Run the benchmarks234 for cmd in commands {235 run_benchmark(&cmd, shell_spawning_time, &options)?;236 }237238 Ok(())239}240241pub struct HyperfineOptions {242 pub warmup_count: Option<u64>,243 pub min_runs: u64,244 pub min_time_sec: Second,
49 unmodified lines
294 if let Some(min_runs) = matches.value_of("min-runs").and_then(&str_to_u64) {295 options.min_runs = cmp::max(1, min_runs);296 }297243 // Run the benchmarks244 let commands = matches.values_of("command").unwrap();245 for cmd in commands {246 let res = run_benchmark(&cmd, &options);298 let commands = matches.values_of("command").unwrap().collect();299 let res = run(&commands, &options);300248 match res {249 Err(err) => error(err.description()),250 Ok(_) => {}251 }301 match res {302 Err(e) => error(e.description()),303 Ok(_) => {}304 }305}