Code style update
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent d4ebdd7 · Browse files · 1 file·+22 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+22 added−17 removed
- other:+22 added−17 removed
Provenance
- Commit
197f9fb- Tree
0a429f1- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
d4ebdd7
1 file changed+22 added−17 removed
modified:src/main.rs+22 added-17 removed
8 unmodified lines9 use ansi_term::Colour::{Cyan, Green, Red};10 use clap::{App, AppSettings, Arg};11 12 struct CmdResult {13- duration_sec: f64,13+ /// Execution time in seconds14+ execution_time_sec: f64,15+16+ /// True if the command finished with exit code zero17 success: bool,18 }19 20 impl CmdResult {18- fn new(duration_sec: f64, success: bool) -> CmdResult {21+ fn new(execution_time_sec: f64, success: bool) -> CmdResult {22 CmdResult {20- duration_sec,23+ execution_time_sec,24 success,25 }26 }27 }28 29+/// Run the given shell command and measure the execution time26-fn run_shell_command(shell_cmd: &str) -> CmdResult {30+fn time_shell_command(shell_cmd: &str) -> CmdResult {31 let start = Instant::now();32 33 let status = Command::new("sh")34 .arg("-c")4 unmodified lines39 .expect("failed to execute process");40 41 let duration = start.elapsed();42 39- let duration_sec = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;43+ let execution_time_sec = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;44 41- CmdResult::new(duration_sec, status.success())45+ CmdResult::new(execution_time_sec, status.success())46 }47 48 fn main() {49 let matches = App::new("hyperfine")11 unmodified lines61 62 let min_time_sec = 5.0;63 let min_runs = 10;64 65+ let progressbar_style = ProgressStyle::default_spinner()66+ .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏")67+ .template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise}");68+69 let commands = matches.values_of("command").unwrap();70+71 for cmd in commands {72 println!("Command: {}", Cyan.paint(cmd));73 println!();74 75 let mut results = vec![];76 77 // Set up progress bar78 let bar = ProgressBar::new(min_runs);70- bar.set_style(71- ProgressStyle::default_spinner()72- .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏")73- .template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise}"),74- );79+ bar.set_style(progressbar_style.clone());80 bar.enable_steady_tick(80);81 bar.set_message("Initial time measurement");82 83 // Initial run79- let res = run_shell_command(cmd);84+ let res = time_shell_command(cmd);85 81- let runs_in_min_time = (min_time_sec / res.duration_sec) as u64;86+ let runs_in_min_time = (min_time_sec / res.execution_time_sec) as u64;87 88 let count = if runs_in_min_time >= min_runs {89 runs_in_min_time90 } else {6 unmodified lines97 bar.set_message("Collecting statistics");98 99 for _ in 1..count {100 bar.inc(1);96- let res = run_shell_command(cmd);101+ let res = time_shell_command(cmd);102 results.push(res);103 }104 bar.finish_and_clear();105 101- let t_sum: f64 = results.iter().map(|r| r.duration_sec).sum();106+ let t_sum: f64 = results.iter().map(|r| r.execution_time_sec).sum();107 let t_mean = t_sum / (results.len() as f64);108 104- let t2_sum: f64 = results.iter().map(|r| r.duration_sec.powi(2)).sum();109+ let t2_sum: f64 = results.iter().map(|r| r.execution_time_sec.powi(2)).sum();110 let t2_mean = t2_sum / (results.len() as f64);111 112 let stddev = (t2_mean - t_mean.powi(2)).sqrt();113 109- let time_fmt = format!("({:.3} ± {:.3}) s", t_mean, stddev);114+ let time_fmt = format!("{:.3} s ± {:.3} s", t_mean, stddev);115 116 println!(" Time: {}", Green.paint(time_fmt));117 118 if !results.iter().all(|r| r.success) {9 unmodified lines