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
Provenance
- Commit
984cec7- Tree
d9a6c94- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
7e41c2a
1 file changed+69 added−17 removed
modified:src/main.rs+69 added-17 removed
85 unmodified lines86 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 }97 98+/// Calculate statistical average99+fn mean<I>(values: I) -> f64100+where101+ 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+}111+112 /// Run the benchmark for a single shell command99-fn run_benchmark(cmd: &str, options: &HyperfineOptions) -> io::Result<()> {113+fn run_benchmark(114+ cmd: &str,115+ shell_spawning_time: Second,116+ options: &HyperfineOptions,117+) -> io::Result<()> {118 println!("Command: {}", Cyan.paint(cmd));119 println!();120 121 let mut results = vec![];38 unmodified lines160 }161 bar.finish_and_clear();162 163 // 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+ };171 148- // Iterator over execution times172+ // Iterator over (corrected) execution times173 let execution_times = results.iter().map(&get_execution_time);174 151- 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)));177 154- let t2_sum: Second = execution_times.clone().map(|t| t.powi(2)).sum();155- let t2_mean = t2_sum / (results.len() as f64);156-178 let stddev = (t2_mean - t_mean.powi(2)).sqrt();179 180 // Formatting and console output181 let time_fmt = format!("{:.3} s ± {:.3} s", t_mean, stddev);21 unmodified lines203 204 Ok(())205 }206 207+// Measure the average shell spawning time208+fn mean_shell_spawning_time() -> io::Result<Second> {209+ const COUNT: u64 = 200;210+ let bar = get_progress_bar(COUNT, "Measuring shell spawning time");211+212+ 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();219+220+ let mean: f64 = mean(times.iter().cloned()); // TODO: get rid of .cloned()221+ Ok(mean)222+}223+224+/// Runs the benchmark for the given commands225+fn run(commands: &Vec<&str>, options: &HyperfineOptions) -> io::Result<()> {226+ let shell_spawning_time = mean_shell_spawning_time()?;227+228+ println!(229+ "Mean shell spawning time: {:.3} ms",230+ 1e3 * shell_spawning_time231+ );232+233+ // Run the benchmarks234+ for cmd in commands {235+ run_benchmark(&cmd, shell_spawning_time, &options)?;236+ }237+238+ Ok(())239+}240+241 pub struct HyperfineOptions {242 pub warmup_count: Option<u64>,243 pub min_runs: u64,244 pub min_time_sec: Second,49 unmodified lines294 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 }297 243- // 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);300 248- match res {249- Err(err) => error(err.description()),250- Ok(_) => {}251- }301+ match res {302+ Err(e) => error(e.description()),303+ Ok(_) => {}304 }305 }