mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 17:00:18 +00:00
Merge pull request #971 from sharkdp/codex/typed-relative-speed
Use typed quantities for relative-speed calculations
This commit is contained in:
1 file changed
+20
-28
@@ -1,8 +1,10 @@
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use super::benchmark_result::BenchmarkResult;
|
use super::benchmark_result::BenchmarkResult;
|
||||||
use crate::options::SortOrder;
|
use crate::{
|
||||||
use crate::quantity::second;
|
options::SortOrder,
|
||||||
|
quantity::{self, Ratio, Time, Zero},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct BenchmarkResultWithRelativeSpeed<'a> {
|
pub struct BenchmarkResultWithRelativeSpeed<'a> {
|
||||||
@@ -16,8 +18,7 @@ pub struct BenchmarkResultWithRelativeSpeed<'a> {
|
|||||||
|
|
||||||
pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {
|
pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {
|
||||||
l.mean_wall_clock_time()
|
l.mean_wall_clock_time()
|
||||||
.get::<second>()
|
.partial_cmp(&r.mean_wall_clock_time())
|
||||||
.partial_cmp(&r.mean_wall_clock_time().get::<second>())
|
|
||||||
.unwrap_or(Ordering::Equal)
|
.unwrap_or(Ordering::Equal)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ fn compute_relative_speeds<'a>(
|
|||||||
let is_reference = result == reference;
|
let is_reference = result == reference;
|
||||||
let relative_ordering = compare_mean_time(result, reference);
|
let relative_ordering = compare_mean_time(result, reference);
|
||||||
|
|
||||||
if result.mean_wall_clock_time().get::<second>() == 0.0 {
|
if result.mean_wall_clock_time() == Time::zero() {
|
||||||
return BenchmarkResultWithRelativeSpeed {
|
return BenchmarkResultWithRelativeSpeed {
|
||||||
result,
|
result,
|
||||||
relative_speed: if is_reference { 1.0 } else { f64::INFINITY },
|
relative_speed: if is_reference { 1.0 } else { f64::INFINITY },
|
||||||
@@ -50,34 +51,25 @@ fn compute_relative_speeds<'a>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let ratio = match relative_ordering {
|
let ratio = match relative_ordering {
|
||||||
Ordering::Less => {
|
Ordering::Less => reference.mean_wall_clock_time() / result.mean_wall_clock_time(),
|
||||||
reference.mean_wall_clock_time().get::<second>()
|
Ordering::Equal => Ratio::new::<quantity::ratio>(1.0),
|
||||||
/ result.mean_wall_clock_time().get::<second>()
|
|
||||||
}
|
|
||||||
Ordering::Equal => 1.0,
|
|
||||||
Ordering::Greater => {
|
Ordering::Greater => {
|
||||||
result.mean_wall_clock_time().get::<second>()
|
result.mean_wall_clock_time() / reference.mean_wall_clock_time()
|
||||||
/ reference.mean_wall_clock_time().get::<second>()
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas
|
// https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas
|
||||||
// Covariance assumed to be 0, i.e. variables are assumed to be independent
|
// Covariance assumed to be 0, i.e. variables are assumed to be independent
|
||||||
let ratio_stddev = match (
|
let ratio_stddev = match (
|
||||||
result
|
result.measurements.stddev(),
|
||||||
.measurements
|
reference.measurements.stddev(),
|
||||||
.stddev()
|
|
||||||
.map(|time| time.get::<second>()),
|
|
||||||
reference
|
|
||||||
.measurements
|
|
||||||
.stddev()
|
|
||||||
.map(|time| time.get::<second>()),
|
|
||||||
) {
|
) {
|
||||||
(Some(result_stddev), Some(fastest_stddev)) => Some(
|
(Some(result_stddev), Some(fastest_stddev)) => Some(
|
||||||
ratio
|
ratio
|
||||||
* ((result_stddev / result.mean_wall_clock_time().get::<second>()).powi(2)
|
* ((result_stddev / result.mean_wall_clock_time())
|
||||||
+ (fastest_stddev / reference.mean_wall_clock_time().get::<second>())
|
.powi(uom::typenum::P2::new())
|
||||||
.powi(2))
|
+ (fastest_stddev / reference.mean_wall_clock_time())
|
||||||
|
.powi(uom::typenum::P2::new()))
|
||||||
.sqrt(),
|
.sqrt(),
|
||||||
),
|
),
|
||||||
_ => None,
|
_ => None,
|
||||||
@@ -85,8 +77,8 @@ fn compute_relative_speeds<'a>(
|
|||||||
|
|
||||||
BenchmarkResultWithRelativeSpeed {
|
BenchmarkResultWithRelativeSpeed {
|
||||||
result,
|
result,
|
||||||
relative_speed: ratio,
|
relative_speed: ratio.get::<quantity::ratio>(),
|
||||||
relative_speed_stddev: ratio_stddev,
|
relative_speed_stddev: ratio_stddev.map(|r| r.get::<quantity::ratio>()),
|
||||||
is_reference,
|
is_reference,
|
||||||
relative_ordering,
|
relative_ordering,
|
||||||
}
|
}
|
||||||
@@ -108,8 +100,8 @@ pub fn compute_with_check_from_reference<'a>(
|
|||||||
reference: &'a BenchmarkResult,
|
reference: &'a BenchmarkResult,
|
||||||
sort_order: SortOrder,
|
sort_order: SortOrder,
|
||||||
) -> Option<Vec<BenchmarkResultWithRelativeSpeed<'a>>> {
|
) -> Option<Vec<BenchmarkResultWithRelativeSpeed<'a>>> {
|
||||||
if fastest_of(results).mean_wall_clock_time().get::<second>() == 0.0
|
if fastest_of(results).mean_wall_clock_time() == Time::zero()
|
||||||
|| reference.mean_wall_clock_time().get::<second>() == 0.0
|
|| reference.mean_wall_clock_time() == Time::zero()
|
||||||
{
|
{
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
@@ -123,7 +115,7 @@ pub fn compute_with_check(
|
|||||||
) -> Option<Vec<BenchmarkResultWithRelativeSpeed<'_>>> {
|
) -> Option<Vec<BenchmarkResultWithRelativeSpeed<'_>>> {
|
||||||
let fastest = fastest_of(results);
|
let fastest = fastest_of(results);
|
||||||
|
|
||||||
if fastest.mean_wall_clock_time().get::<second>() == 0.0 {
|
if fastest.mean_wall_clock_time() == Time::zero() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user