Further simplification of the API
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent 4badeb6 · Browse files · 4 files·+49 added−93 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+49 added−93 removed
- other:+49 added−93 removed
Provenance
- Commit
ebf1273- Tree
ff1b9a8- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <mail@david-peter.de>- Parent
4badeb6
4 files changed+49 added−93 removed
modified:src/benchmark/mod.rs+6 added-3 removed
363 unmodified lines364 bar.finish_and_clear()365 }366 367 // Formatting and console output368- let (mean_str, time_unit) = measurements369- .time_wall_clock_mean()370- .format_auto_with_unit(self.options.time_unit);368+ let t_wall_clock_mean = measurements.time_wall_clock_mean();369+ let time_unit = self370+ .options371+ .time_unit372+ .unwrap_or(t_wall_clock_mean.suitable_unit());373+ let mean_str = t_wall_clock_mean.format_auto(Some(time_unit));374 let min_str = measurements.min().format_auto(Some(time_unit));375 let max_str = measurements.max().format_auto(Some(time_unit));376 let num_str = format!("{num_runs} runs", num_runs = measurements.len());377 115 unmodified linesmodified:src/export/markup.rs+3 added-7 removed
51 unmodified lines52 // prepare data row strings53 let cmd_str = result.command_with_unused_parameters().replace('|', "\\|");54 let mean_str = result55 .mean_wall_clock_time()56- .format_value_auto(Some(time_unit))57- .0;56+ .format_value_auto(Some(time_unit));57 let stddev_str = if let Some(stddev) = result.measurements.stddev() {59- format!(" ± {}", stddev.format_value_auto(Some(time_unit)).0)58+ format!(" ± {}", stddev.format_value_auto(Some(time_unit)))59 } else {60 "".into()61 };62 let min_str = result.measurements.min().format_value(time_unit);52 unmodified lines115 116 fn determine_unit_from_results(results: &[BenchmarkResult]) -> TimeUnit {117 if let Some(first_result) = results.first() {118 // Use the first BenchmarkResult entry to determine the unit for all entries.120- first_result121- .mean_wall_clock_time()122- .format_auto_with_unit(None)123- .1119+ first_result.mean_wall_clock_time().suitable_unit()120 } else {121 // Default to `Second`.122 TimeUnit::Second123 }25 unmodified linesmodified:src/output/warnings.rs+2 added-2 removed
1 use std::fmt;2 3 use crate::benchmark::MIN_EXECUTION_TIME;4-use crate::quantity::{Time, TimeQuantity, TimeUnit};4+use crate::quantity::{Time, TimeQuantity};5 6 pub struct OutlierWarningOptions {7 pub warmup_in_use: bool,8 pub prepare_in_use: bool,15 unmodified lines24 "Command took less than {min_execution_time} to complete. Note that the results might be \25 inaccurate because hyperfine can not calibrate the shell startup time much \26 more precise than this limit. You can try to use the `-N`/`--shell=none` \27 option to disable the shell completely.",28- min_execution_time = MIN_EXECUTION_TIME.format_value_in(TimeUnit::MilliSecond, 0)28+ min_execution_time = MIN_EXECUTION_TIME.format_auto(None)29 ),30 Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),31 Warnings::SlowInitialRun(time_first_run, ref options) => write!(32 f,36 unmodified linesmodified:src/quantity/mod.rs+38 added-81 removed
30 unmodified lines31 fn value_in<U>(&self, u: U) -> f6432 where33 U: si::time::Unit + Conversion<f64, T = f64>;34 35- fn format_value_in(&self, time_unit: TimeUnit, precision: usize) -> String;35+ fn suitable_unit(&self) -> TimeUnit;36+37 fn format_auto(&self, time_unit: Option<TimeUnit>) -> String;37- fn format_auto_with_unit(&self, time_unit: Option<TimeUnit>) -> (String, TimeUnit);38- fn format_value_auto(&self, time_unit: Option<TimeUnit>) -> (String, TimeUnit);38+ fn format_value_auto(&self, time_unit: Option<TimeUnit>) -> String;39 }40 41+fn format_value_in(t: Time, u: TimeUnit, precision: Option<usize>) -> String {42+ let precision = precision.unwrap_or_else(|| if u == TimeUnit::Second { 3 } else { 1 });43+ u.format(t, precision)44+}45+46 impl TimeQuantity for Time {47 fn value_in<U>(&self, _u: U) -> f6448 where49 U: si::time::Unit + Conversion<f64, T = f64>,50 {51 self.get::<U>()52 }53 49- fn format_value_in(&self, u: TimeUnit, precision: usize) -> String {50- u.format(*self, precision)54+ fn suitable_unit(&self) -> TimeUnit {55+ if *self < Time::new::<millisecond>(1.0) {56+ TimeUnit::MicroSecond57+ } else if *self < Time::new::<second>(1.0) {58+ TimeUnit::MilliSecond59+ } else {60+ TimeUnit::Second61+ }62 }63 64 /// Format the given duration as a string. The output-unit can be enforced by setting `unit` to65 /// `Some(target_unit)`. If `unit` is `None`, it will be determined automatically.66 fn format_auto(&self, time_unit: Option<TimeUnit>) -> String {56- self.format_auto_with_unit(time_unit).057- }67+ let time_unit = time_unit.unwrap_or_else(|| self.suitable_unit());68 59- /// Like `format_duration`, but returns the target unit as well.60- fn format_auto_with_unit(&self, time_unit: Option<TimeUnit>) -> (String, TimeUnit) {61- let (out_str, out_unit) = self.format_value_auto(time_unit);69+ let value = format_value_in(*self, time_unit, None);70 63- (format!("{} {}", out_str, out_unit.short_name()), out_unit)71+ format!("{} {}", value, time_unit.short_name())72 }73 74 /// Like `format_duration`, but returns the target unit as well.67- fn format_value_auto(&self, time_unit: Option<TimeUnit>) -> (String, TimeUnit) {68- let (time_unit, precision) = if (*self < Time::new::<millisecond>(1.0)69- && time_unit.is_none())70- || time_unit == Some(TimeUnit::MicroSecond)71- {72- (TimeUnit::MicroSecond, 1)73- } else if (*self < Time::new::<second>(1.0) && time_unit.is_none())74- || time_unit == Some(TimeUnit::MilliSecond)75- {76- (TimeUnit::MilliSecond, 1)77- } else {78- let time_unit = time_unit.unwrap_or(TimeUnit::Second);79- let precision = if time_unit == TimeUnit::Second { 3 } else { 1 };75+ fn format_value_auto(&self, time_unit: Option<TimeUnit>) -> String {76+ let time_unit = time_unit.unwrap_or_else(|| self.suitable_unit());77 81- (time_unit, precision)82- };83-84- (self.format_value_in(time_unit, precision), time_unit)78+ format_value_in(*self, time_unit, None)79 }80 }81 82 pub const fn const_time_from_seconds(value: f64) -> Time {121 unmodified lines204 assert_eq!(result.format_auto(Some(TimeUnit::MilliSecond)), "178.9 ms");205 }206 207 #[test]214-fn test_format_duration() {215- let (out_str, out_unit) = Time::new::<second>(1.3).format_auto_with_unit(None);216-217-#[test]218-fn test_suitable_unit_information() {208+fn test_suiteable_unit() {209+ assert_eq!(Time::new::<second>(1.3).suitable_unit(), TimeUnit::Second);210+ assert_eq!(Time::new::<second>(1.0).suitable_unit(), TimeUnit::Second);211 assert_eq!(220- Information::new::<byte>(512.0).suitable_unit(),221- InformationUnit::Byte212+ Time::new::<second>(0.999).suitable_unit(),213+ TimeUnit::MilliSecond214 );215 assert_eq!(224- Information::new::<byte>(2048.0).suitable_unit(),225- InformationUnit::KibiByte216+ Time::new::<second>(0.0005).suitable_unit(),217+ TimeUnit::MicroSecond218 );219 assert_eq!(228- Information::new::<byte>(2_097_152.0).suitable_unit(),229- InformationUnit::MebiByte220+ Time::new::<second>(0.).suitable_unit(),221+ TimeUnit::MicroSecond222 );223 assert_eq!(232- Information::new::<byte>(2_147_483_648.0).suitable_unit(),233- InformationUnit::GibiByte224+ Time::new::<second>(1000.0).suitable_unit(),225+ TimeUnit::Second226 );227 }228 237- let (out_str, out_unit) = Time::new::<second>(1.0).format_auto_with_unit(None);238-239- assert_eq!("1.000 s", out_str);240- assert_eq!(TimeUnit::Second, out_unit);241-242- let (out_str, out_unit) = Time::new::<second>(0.999).format_auto_with_unit(None);243-244- assert_eq!("999.0 ms", out_str);245- assert_eq!(TimeUnit::MilliSecond, out_unit);246-247- let (out_str, out_unit) = Time::new::<second>(0.0005).format_auto_with_unit(None);248-249- assert_eq!("500.0 µs", out_str);250- assert_eq!(TimeUnit::MicroSecond, out_unit);251-252- let (out_str, out_unit) = Time::new::<second>(0.).format_auto_with_unit(None);253-254- assert_eq!("0.0 µs", out_str);255- assert_eq!(TimeUnit::MicroSecond, out_unit);256-257- let (out_str, out_unit) = Time::new::<second>(1000.0).format_auto_with_unit(None);258-259- assert_eq!("1000.000 s", out_str);260- assert_eq!(TimeUnit::Second, out_unit);261-}262-229 #[test]230 fn test_format_duration_unit_with_unit() {265- let (out_str, out_unit) =266- Time::new::<second>(1.3).format_auto_with_unit(Some(TimeUnit::Second));231+ let out = Time::new::<second>(1.3).format_auto(Some(TimeUnit::Second));232+ assert_eq!("1.300 s", out);233 268- let out = Time::new::<second>(1.3).format(TimeUnit::MilliSecond);234+ let out = Time::new::<second>(1.3).format_auto(Some(TimeUnit::MilliSecond));235 assert_eq!("1300.0 ms", out);236 271- let (out_str, out_unit) =272- Time::new::<second>(1.3).format_auto_with_unit(Some(TimeUnit::MilliSecond));273-274- assert_eq!("1300.0 ms", out_str);275- assert_eq!(TimeUnit::MilliSecond, out_unit);276-277- let (out_str, out_unit) =278- Time::new::<second>(1.3).format_auto_with_unit(Some(TimeUnit::MicroSecond));279-280- assert_eq!("1300000.0 µs", out_str);281- assert_eq!(TimeUnit::MicroSecond, out_unit);237+ let out = Time::new::<second>(1.3).format_auto(Some(TimeUnit::MicroSecond));238+ assert_eq!("1300000.0 µs", out);239 }