Skip to content
Myr

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

1 file+22 added−17 removed

modified:src/main.rs+22 added-17 removed
8 unmodified lines
9use ansi_term::Colour::{Cyan, Green, Red};10use clap::{App, AppSettings, Arg};1112struct CmdResult {13 duration_sec: f64,13 /// Execution time in seconds14 execution_time_sec: f64,1516 /// True if the command finished with exit code zero17 success: bool,18}1920impl 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}2829/// Run the given shell command and measure the execution time26fn run_shell_command(shell_cmd: &str) -> CmdResult {30fn time_shell_command(shell_cmd: &str) -> CmdResult {31 let start = Instant::now();3233 let status = Command::new("sh")34 .arg("-c")
4 unmodified lines
39 .expect("failed to execute process");4041 let duration = start.elapsed();4239 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;4441 CmdResult::new(duration_sec, status.success())45 CmdResult::new(execution_time_sec, status.success())46}4748fn main() {49 let matches = App::new("hyperfine")
11 unmodified lines
6162 let min_time_sec = 5.0;63 let min_runs = 10;6465 let progressbar_style = ProgressStyle::default_spinner()66 .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏")67 .template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise}");6869 let commands = matches.values_of("command").unwrap();7071 for cmd in commands {72 println!("Command: {}", Cyan.paint(cmd));73 println!();7475 let mut results = vec![];7677 // 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");8283 // Initial run79 let res = run_shell_command(cmd);84 let res = time_shell_command(cmd);8581 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;8788 let count = if runs_in_min_time >= min_runs {89 runs_in_min_time90 } else {
6 unmodified lines
97 bar.set_message("Collecting statistics");9899 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();105101 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);108104 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);111112 let stddev = (t2_mean - t_mean.powi(2)).sqrt();113109 let time_fmt = format!("({:.3} ± {:.3}) s", t_mean, stddev);114 let time_fmt = format!("{:.3} s ± {:.3} s", t_mean, stddev);115116 println!(" Time: {}", Green.paint(time_fmt));117118 if !results.iter().all(|r| r.success) {
9 unmodified lines