mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 17:00:18 +00:00
Use uom quantities for benchmark measurements
This commit is contained in:
1 parent
fbdb75efc3
commit
a8e56efecc
14 files changed
+301
-118
No files matched your search
+111
-27
@@ -1,5 +1,9 @@
|
||||
//! Statistics for non-empty samples of finite measurements.
|
||||
|
||||
use std::ops::{Add, AddAssign, Div};
|
||||
|
||||
use crate::quantity::{byte, ratio, second, Information, Ratio, Time, Zero};
|
||||
|
||||
/// A min function that assumes no NaNs and at least one element
|
||||
pub fn min<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {
|
||||
values
|
||||
@@ -17,54 +21,105 @@ pub fn max<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {
|
||||
}
|
||||
|
||||
/// Arithmetic mean, summing measurements in their original order.
|
||||
pub fn mean(values: impl IntoIterator<Item = f64>) -> f64 {
|
||||
let mut sum = 0.0;
|
||||
pub fn mean<Q, P>(values: impl IntoIterator<Item = Q>) -> Q
|
||||
where
|
||||
Q: AddAssign + Zero + Div<Ratio, Output = P>,
|
||||
P: Into<Q>,
|
||||
{
|
||||
let mut sum = Q::zero();
|
||||
let mut count = 0usize;
|
||||
for value in values {
|
||||
sum += value;
|
||||
count += 1;
|
||||
}
|
||||
sum / count as f64
|
||||
|
||||
let count = Ratio::new::<ratio>(count as f64);
|
||||
(sum / count).into()
|
||||
}
|
||||
|
||||
/// Median without modifying the input sample.
|
||||
pub fn median(values: &[f64]) -> f64 {
|
||||
pub fn median<Q, P>(values: impl IntoIterator<Item = Q>) -> Q
|
||||
where
|
||||
Q: Copy + PartialOrd + Add<Output = Q> + Div<Ratio, Output = P>,
|
||||
P: Into<Q>,
|
||||
{
|
||||
let mut values = values.into_iter().collect::<Vec<_>>();
|
||||
assert!(
|
||||
!values.is_empty(),
|
||||
"median requires at least one measurement"
|
||||
);
|
||||
let mut sorted = values.to_vec();
|
||||
sorted.sort_unstable_by(|a, b| a.partial_cmp(b).expect("No NaN measurements"));
|
||||
values.sort_unstable_by(|a, b| a.partial_cmp(b).expect("No NaN measurements"));
|
||||
|
||||
let mid = sorted.len() / 2;
|
||||
if sorted.len() % 2 == 1 {
|
||||
sorted[mid]
|
||||
let len = values.len();
|
||||
if len % 2 == 0 {
|
||||
let mid = len / 2;
|
||||
let a = &values[mid - 1];
|
||||
let b = &values[mid];
|
||||
((*b + *a) / Ratio::new::<ratio>(2.)).into()
|
||||
} else {
|
||||
(sorted[mid] + sorted[mid - 1]) / 2.0
|
||||
values[len / 2]
|
||||
}
|
||||
}
|
||||
|
||||
/// Access a quantity in its base unit for statistical operations.
|
||||
pub trait RawValue {
|
||||
fn raw_value(&self) -> f64;
|
||||
fn from_raw_value(value: f64) -> Self;
|
||||
}
|
||||
|
||||
impl RawValue for Time {
|
||||
fn raw_value(&self) -> f64 {
|
||||
self.get::<second>()
|
||||
}
|
||||
|
||||
fn from_raw_value(value: f64) -> Self {
|
||||
Time::new::<second>(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl RawValue for Information {
|
||||
fn raw_value(&self) -> f64 {
|
||||
self.get::<byte>()
|
||||
}
|
||||
|
||||
fn from_raw_value(value: f64) -> Self {
|
||||
Information::new::<byte>(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl RawValue for f64 {
|
||||
fn raw_value(&self) -> f64 {
|
||||
*self
|
||||
}
|
||||
fn from_raw_value(value: f64) -> Self {
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
/// Sample standard deviation, using an already computed mean.
|
||||
pub fn standard_deviation(values: &[f64], mean: f64) -> f64 {
|
||||
pub fn standard_deviation<Q: RawValue>(values: &[Q], mean: Q) -> Q {
|
||||
let mean = mean.raw_value();
|
||||
assert!(
|
||||
values.len() > 1,
|
||||
"standard deviation requires at least two measurements"
|
||||
);
|
||||
let sum = values
|
||||
.iter()
|
||||
.map(|value| (value - mean) * (value - mean))
|
||||
.map(|value| (value.raw_value() - mean) * (value.raw_value() - mean))
|
||||
.fold(0.0, |sum, deviation| sum + deviation);
|
||||
assert!(sum >= 0.0, "invalid sum of squared deviations");
|
||||
|
||||
// Preserve the operation order of statistical 1.0: dividing by n - 1
|
||||
// before taking the square root also preserves exported floating-point values.
|
||||
(sum / (values.len() as f64 - 1.0)).sqrt()
|
||||
Q::from_raw_value((sum / (values.len() as f64 - 1.0)).sqrt())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use approx::assert_relative_eq;
|
||||
use uom::si::information::kibibyte;
|
||||
use uom::si::time::{microsecond, millisecond};
|
||||
|
||||
#[test]
|
||||
fn test_min() {
|
||||
@@ -74,8 +129,12 @@ mod tests {
|
||||
assert_eq!(-1.0, min([-1.0, 1.0]));
|
||||
assert_eq!(-1.0, min([1.0, -1.0, 0.0]));
|
||||
|
||||
let values = vec![1024.0, 2.0, 3.0];
|
||||
assert_eq!(*min(&values), 2.0);
|
||||
let values = vec![
|
||||
Information::new::<kibibyte>(1.0),
|
||||
Information::new::<byte>(2.0),
|
||||
Information::new::<byte>(3.0),
|
||||
];
|
||||
assert_eq!(min(&values).get::<byte>(), 2.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -86,8 +145,12 @@ mod tests {
|
||||
assert_eq!(1.0, max([-1.0, 1.0]));
|
||||
assert_eq!(1.0, max([-1.0, 1.0, 0.0]));
|
||||
|
||||
let values = vec![1.0, 2048.0, 3.0];
|
||||
assert_eq!(*max(&values), 2048.0);
|
||||
let values = vec![
|
||||
Information::new::<byte>(1.0),
|
||||
Information::new::<kibibyte>(2.0),
|
||||
Information::new::<byte>(3.0),
|
||||
];
|
||||
assert_eq!(max(&values).get::<kibibyte>(), 2.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -95,25 +158,46 @@ mod tests {
|
||||
assert_eq!(1.0, mean([1.0]));
|
||||
assert_relative_eq!(2.0, mean([1.0, 3.0]));
|
||||
|
||||
let values = [0.1, 0.2, 0.6];
|
||||
assert_relative_eq!(mean(values), 0.3);
|
||||
let values = [
|
||||
Time::new::<millisecond>(100.0),
|
||||
Time::new::<millisecond>(200.0),
|
||||
Time::new::<microsecond>(600_000.0),
|
||||
];
|
||||
let result = mean(values);
|
||||
assert_relative_eq!(result.get::<millisecond>(), 300.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_median() {
|
||||
assert_eq!(1.0, median(&[1.0]));
|
||||
assert_relative_eq!(2.0, median(&[1.0, 3.0]));
|
||||
assert_eq!(1.0, median([1.0]));
|
||||
assert_relative_eq!(2.0, median([1.0, 3.0]));
|
||||
|
||||
let values = [0.1, 0.2, 0.6];
|
||||
assert_relative_eq!(median(&values), 0.2);
|
||||
let values = [
|
||||
Time::new::<millisecond>(100.0),
|
||||
Time::new::<millisecond>(200.0),
|
||||
Time::new::<microsecond>(600_000.0),
|
||||
];
|
||||
let result = median(values);
|
||||
assert_relative_eq!(result.get::<millisecond>(), 200.0);
|
||||
|
||||
let values = [0.1, 0.2, 0.3, 0.6];
|
||||
assert_relative_eq!(median(&values), 0.25);
|
||||
let values = [
|
||||
Time::new::<millisecond>(100.0),
|
||||
Time::new::<millisecond>(200.0),
|
||||
Time::new::<microsecond>(300_000.0),
|
||||
Time::new::<microsecond>(600_000.0),
|
||||
];
|
||||
let result = median(values);
|
||||
assert_relative_eq!(result.get::<millisecond>(), 250.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_standard_deviation() {
|
||||
let values = [0.1, 0.2, 0.3];
|
||||
assert_relative_eq!(standard_deviation(&values, mean(values)), 0.1);
|
||||
let values = [
|
||||
Time::new::<millisecond>(100.0),
|
||||
Time::new::<millisecond>(200.0),
|
||||
Time::new::<microsecond>(300_000.0),
|
||||
];
|
||||
let result = standard_deviation(&values, mean(values));
|
||||
assert_relative_eq!(result.get::<millisecond>(), 100.0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user