Skip to content
Myr

Use typed quantities for relative-speed calculations

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 4aa36c8 · 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

1 file+20 added−28 removed

modified:src/benchmark/relative_speed.rs+20 added-28 removed
1use std::cmp::Ordering;23use super::benchmark_result::BenchmarkResult;4use crate::options::SortOrder;5use crate::quantity::second;4use crate::{5 options::SortOrder,6 quantity::{self, Ratio, Time, Zero},7};89#[derive(Debug)]10pub struct BenchmarkResultWithRelativeSpeed<'a> {11 pub result: &'a BenchmarkResult,
5 unmodified lines
17}1819pub 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}2425pub fn fastest_of(results: &[BenchmarkResult]) -> &BenchmarkResult {
13 unmodified lines
39 .map(|result| {40 let is_reference = result == reference;41 let relative_ordering = compare_mean_time(result, reference);4242 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 lines
50 };51 }5253 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 };6061 // 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 };7778 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 lines
99 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 lines
114 sort_order: SortOrder,115) -> Option<Vec<BenchmarkResultWithRelativeSpeed<'_>>> {116 let fastest = fastest_of(results);117126 if fastest.mean_wall_clock_time().get::<second>() == 0.0 {118 if fastest.mean_wall_clock_time() == Time::zero() {119 return None;120 }121122 Some(compute_relative_speeds(results, fastest, sort_order))
72 unmodified lines