Skip to content
Myr

Refactoring

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

authored by sharkdp · committed by sharkdp · parent f8cbb33 · Browse files · 1 file+94 added−73 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+94 added−73 removed

  • other:+94 added−73 removed

1 file+94 added−73 removed

modified:src/main.rs+94 added-73 removed
57 unmodified lines
5859 bar60}6162/// Run the benchmark for a single shell command63fn run_benchmark(cmd: &str, options: &HyperfineOptions) {64 println!("Command: {}", Cyan.paint(cmd));65 println!();6667 let mut results = vec![];6869 // Warmup phase70 if let Some(warmup_count) = options.warmup_count71 {72 let bar = get_progress_bar(warmup_count, "Performing warmup runs");7374 for _ in 1..warmup_count {75 bar.inc(1);76 let _ = time_shell_command(cmd);77 }78 bar.finish_and_clear();79 }8081 // Set up progress bar (and spinner for initial measurement)82 let bar = get_progress_bar(options.min_runs, "Initial time measurement");8384 // Initial timing run85 let res = time_shell_command(cmd);8687 let runs_in_min_time = (options.min_time_sec / res.execution_time_sec) as u64;8889 let count = if runs_in_min_time >= options.min_runs {90 runs_in_min_time91 } else {92 options.min_runs93 };9495 // Save the first result96 results.push(res);9798 // Re-configure the progress bar99 bar.set_length(count);100 bar.set_message("Collecting statistics");101102 // Gather statistics103 for _ in 1..count {104 bar.inc(1);105 let res = time_shell_command(cmd);106 results.push(res);107 }108 bar.finish_and_clear();109110 // Compute statistical quantities111 let t_sum: f64 = results.iter().map(|r| r.execution_time_sec).sum();112 let t_mean = t_sum / (results.len() as f64);113114 let t2_sum: f64 = results.iter().map(|r| r.execution_time_sec.powi(2)).sum();115 let t2_mean = t2_sum / (results.len() as f64);116117 let stddev = (t2_mean - t_mean.powi(2)).sqrt();118119 // Formatting and console output120 let time_fmt = format!("{:.3} s ± {:.3} s", t_mean, stddev);121122 println!(" Time: {}", Green.paint(time_fmt));123124 if !results.iter().all(|r| r.success) {125 println!(126 " {}: Program returned non-zero exit status",127 Red.paint("Warning")128 );129 };130131 println!();132}133134pub struct HyperfineOptions {135 pub warmup_count: Option<u64>,136 pub min_runs: u64,137 pub min_time_sec: f64,138}139140impl Default for HyperfineOptions {141 fn default() -> HyperfineOptions {142 HyperfineOptions {143 warmup_count: None,144 min_runs: 10,145 min_time_sec: 5.0146 }147 }148}149150fn main() {151 let matches = App::new("hyperfine")152 .global_settings(&[AppSettings::ColoredHelp])153 .version("0.1")
14 unmodified lines
168 .help("Perform NUM warmup runs before the actual benchmark"),169 )170 .get_matches();17184 let min_time_sec = 5.0;85 let min_runs = 10;17287 let commands = matches.values_of("command").unwrap();173 let mut options = HyperfineOptions::default();174 options.warmup_count =175 matches.value_of("warmup")176 .and_then(|n| u64::from_str_radix(n, 10).ok());177178 let commands = matches.values_of("command").unwrap();179 for cmd in commands {90 println!("Command: {}", Cyan.paint(cmd));91 println!();9293 let mut results = vec![];9495 // Warmup phase96 if let Some(warmup_count) = matches97 .value_of("warmup")98 .and_then(|n| u64::from_str_radix(n, 10).ok())99 {100 let bar = get_progress_bar(warmup_count, "Performing warmup runs");101102 for _ in 1..warmup_count {103 bar.inc(1);104 let _ = time_shell_command(cmd);105 }106 bar.finish_and_clear();107 }108109 // Set up progress bar (and spinner for initial measurement)110 let bar = get_progress_bar(min_runs, "Initial time measurement");111112 // Initial timing run113 let res = time_shell_command(cmd);114115 let runs_in_min_time = (min_time_sec / res.execution_time_sec) as u64;116117 let count = if runs_in_min_time >= min_runs {118 runs_in_min_time119 } else {120 min_runs121 };122123 // Save the first result124 results.push(res);125126 // Re-configure the progress bar127 bar.set_length(count);128 bar.set_message("Collecting statistics");129130 // Gather statistics131 for _ in 1..count {132 bar.inc(1);133 let res = time_shell_command(cmd);134 results.push(res);135 }136 bar.finish_and_clear();137138 // Compute statistical quantities139 let t_sum: f64 = results.iter().map(|r| r.execution_time_sec).sum();140 let t_mean = t_sum / (results.len() as f64);141142 let t2_sum: f64 = results.iter().map(|r| r.execution_time_sec.powi(2)).sum();143 let t2_mean = t2_sum / (results.len() as f64);144145 let stddev = (t2_mean - t_mean.powi(2)).sqrt();146147 // Formatting and console output148 let time_fmt = format!("{:.3} s ± {:.3} s", t_mean, stddev);149150 println!(" Time: {}", Green.paint(time_fmt));151152 if !results.iter().all(|r| r.success) {153 println!(154 " {}: Program returned non-zero exit status",155 Red.paint("Warning")156 );157 };158159 println!();180 run_benchmark(&cmd, &options);181 }182}