Merge pull request #971 from sharkdp/codex/typed-relative-speed
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Use typed quantities for relative-speed calculations
authored by David Peter · committed by GitHub · parents 4aa36c8, df49a04 · Browse files · 1 file·+20 added−28 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+20 added−28 removed
- other:+20 added−28 removed
Provenance
- Commit
33aafdc- Tree
2b4e2c1- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
4aa36c8- Parent 2
df49a04
1 file changed+20 added−28 removed
modified:src/benchmark/relative_speed.rs+20 added-28 removed
1 use std::cmp::Ordering;2 3 use super::benchmark_result::BenchmarkResult;4-use crate::options::SortOrder;5-use crate::quantity::second;4+use crate::{5+ options::SortOrder,6+ quantity::{self, Ratio, Time, Zero},7+};8 9 #[derive(Debug)]10 pub struct BenchmarkResultWithRelativeSpeed<'a> {11 pub result: &'a BenchmarkResult,5 unmodified lines17 }18 19 pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {20 l.mean_wall_clock_time()19- .get::<second>()20- .partial_cmp(&r.mean_wall_clock_time().get::<second>())21+ .partial_cmp(&r.mean_wall_clock_time())22 .unwrap_or(Ordering::Equal)23 }24 25 pub fn fastest_of(results: &[BenchmarkResult]) -> &BenchmarkResult {13 unmodified lines39 .map(|result| {40 let is_reference = result == reference;41 let relative_ordering = compare_mean_time(result, reference);42 42- if result.mean_wall_clock_time().get::<second>() == 0.0 {43+ if result.mean_wall_clock_time() == Time::zero() {44 return BenchmarkResultWithRelativeSpeed {45 result,46 relative_speed: if is_reference { 1.0 } else { f64::INFINITY },47 relative_speed_stddev: None,2 unmodified lines50 };51 }52 53 let ratio = match relative_ordering {53- Ordering::Less => {54- reference.mean_wall_clock_time().get::<second>()55- / result.mean_wall_clock_time().get::<second>()56- }57- Ordering::Equal => 1.0,54+ Ordering::Less => reference.mean_wall_clock_time() / result.mean_wall_clock_time(),55+ Ordering::Equal => Ratio::new::<quantity::ratio>(1.0),56 Ordering::Greater => {59- result.mean_wall_clock_time().get::<second>()60- / reference.mean_wall_clock_time().get::<second>()57+ result.mean_wall_clock_time() / reference.mean_wall_clock_time()58 }59 };60 61 // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas62 // Covariance assumed to be 0, i.e. variables are assumed to be independent63 let ratio_stddev = match (67- result68- .measurements69- .stddev()70- .map(|time| time.get::<second>()),71- reference72- .measurements73- .stddev()74- .map(|time| time.get::<second>()),64+ result.measurements.stddev(),65+ reference.measurements.stddev(),66 ) {67 (Some(result_stddev), Some(fastest_stddev)) => Some(68 ratio78- * ((result_stddev / result.mean_wall_clock_time().get::<second>()).powi(2)79- + (fastest_stddev / reference.mean_wall_clock_time().get::<second>())80- .powi(2))69+ * ((result_stddev / result.mean_wall_clock_time())70+ .powi(uom::typenum::P2::new())71+ + (fastest_stddev / reference.mean_wall_clock_time())72+ .powi(uom::typenum::P2::new()))73 .sqrt(),74 ),75 _ => None,76 };77 78 BenchmarkResultWithRelativeSpeed {79 result,88- relative_speed: ratio,89- relative_speed_stddev: ratio_stddev,80+ relative_speed: ratio.get::<quantity::ratio>(),81+ relative_speed_stddev: ratio_stddev.map(|r| r.get::<quantity::ratio>()),82 is_reference,83 relative_ordering,84 }85 })13 unmodified lines99 results: &'a [BenchmarkResult],100 reference: &'a BenchmarkResult,101 sort_order: SortOrder,102 ) -> Option<Vec<BenchmarkResultWithRelativeSpeed<'a>>> {111- if fastest_of(results).mean_wall_clock_time().get::<second>() == 0.0112- || reference.mean_wall_clock_time().get::<second>() == 0.0103+ if fastest_of(results).mean_wall_clock_time() == Time::zero()104+ || reference.mean_wall_clock_time() == Time::zero()105 {106 return None;107 }108 5 unmodified lines114 sort_order: SortOrder,115 ) -> Option<Vec<BenchmarkResultWithRelativeSpeed<'_>>> {116 let fastest = fastest_of(results);117 126- if fastest.mean_wall_clock_time().get::<second>() == 0.0 {118+ if fastest.mean_wall_clock_time() == Time::zero() {119 return None;120 }121 122 Some(compute_relative_speeds(results, fastest, sort_order))72 unmodified lines