mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-10-28 08:08:58 +00:00
Unit safe statistics functions
This commit is contained in:
1 parent
f342e1f5de
commit
3a2c8ab177
3 files changed
+88
-40
No files matched your search
@@ -64,21 +64,18 @@ impl Measurements {
|
||||
self.measurements.push(measurement);
|
||||
}
|
||||
|
||||
pub fn wall_clock_times(&self) -> Vec<Time> {
|
||||
self.measurements
|
||||
.iter()
|
||||
.map(|m| m.time_wall_clock)
|
||||
.collect()
|
||||
pub fn wall_clock_times(&self) -> impl Iterator<Item = Time> + '_ {
|
||||
self.measurements.iter().map(|m| m.time_wall_clock)
|
||||
}
|
||||
|
||||
/// The average wall clock time
|
||||
pub fn time_wall_clock_mean(&self) -> Time {
|
||||
mean(&self.wall_clock_times())
|
||||
mean(self.wall_clock_times())
|
||||
}
|
||||
|
||||
/// The standard deviation of all wall clock times. Not available if only one run has been performed
|
||||
pub fn stddev(&self) -> Option<Time> {
|
||||
let times = self.wall_clock_times();
|
||||
let times: Vec<_> = self.wall_clock_times().collect(); // TODO: Avoid collecting
|
||||
|
||||
if times.len() < 2 {
|
||||
None
|
||||
@@ -89,54 +86,36 @@ impl Measurements {
|
||||
|
||||
/// The median wall clock time
|
||||
pub fn median(&self) -> Time {
|
||||
median(&self.wall_clock_times())
|
||||
median(&self.wall_clock_times().collect::<Vec<_>>())
|
||||
}
|
||||
|
||||
/// The minimum wall clock time
|
||||
pub fn min(&self) -> Time {
|
||||
min(&self.wall_clock_times())
|
||||
min(self.wall_clock_times())
|
||||
}
|
||||
|
||||
/// The maximum wall clock time
|
||||
pub fn max(&self) -> Time {
|
||||
max(&self.wall_clock_times())
|
||||
max(self.wall_clock_times())
|
||||
}
|
||||
|
||||
/// Compute modified Z-scores for the wall clock times
|
||||
pub fn modified_zscores(&self) -> Vec<f64> {
|
||||
modified_zscores(&self.wall_clock_times())
|
||||
modified_zscores(&self.wall_clock_times().collect::<Vec<_>>())
|
||||
}
|
||||
|
||||
/// The average user time
|
||||
pub fn time_user_mean(&self) -> Time {
|
||||
mean(
|
||||
&self
|
||||
.measurements
|
||||
.iter()
|
||||
.map(|m| m.time_user)
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
mean(self.measurements.iter().map(|m| m.time_user))
|
||||
}
|
||||
|
||||
/// The average system time
|
||||
pub fn time_system_mean(&self) -> Time {
|
||||
mean(
|
||||
&self
|
||||
.measurements
|
||||
.iter()
|
||||
.map(|m| m.time_system)
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
mean(self.measurements.iter().map(|m| m.time_system))
|
||||
}
|
||||
|
||||
pub fn peak_memory_usage_mean(&self) -> Information {
|
||||
mean(
|
||||
&self
|
||||
.measurements
|
||||
.iter()
|
||||
.map(|m| m.peak_memory_usage)
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
mean(self.measurements.iter().map(|m| m.peak_memory_usage))
|
||||
}
|
||||
|
||||
pub fn peak_memory_usage_mean(&self) -> Information {
|
||||
|
||||
@@ -22,7 +22,7 @@ use crate::output::progress_bar::{
|
||||
};
|
||||
use crate::output::warnings::{OutlierWarningOptions, Warnings};
|
||||
use crate::parameter::ParameterNameAndValue;
|
||||
use crate::quantity::{self, const_time_from_seconds, Time, Quantity};
|
||||
use crate::quantity::{self, const_time_from_seconds, Quantity, Time};
|
||||
use benchmark_result::BenchmarkResult;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
@@ -421,8 +421,7 @@ impl<'a> Benchmark<'a> {
|
||||
if matches!(self.options.executor_kind, ExecutorKind::Shell(_))
|
||||
&& measurements
|
||||
.wall_clock_times()
|
||||
.iter()
|
||||
.any(|&t| t < MIN_EXECUTION_TIME)
|
||||
.any(|t| t < MIN_EXECUTION_TIME)
|
||||
{
|
||||
warnings.push(Warnings::FastExecutionTime);
|
||||
}
|
||||
@@ -448,7 +447,7 @@ impl<'a> Benchmark<'a> {
|
||||
|
||||
if scores[0] > OUTLIER_THRESHOLD {
|
||||
warnings.push(Warnings::SlowInitialRun(
|
||||
measurements.wall_clock_times()[0],
|
||||
measurements.wall_clock_times().next().unwrap(),
|
||||
outlier_warning_options,
|
||||
));
|
||||
} else if scores.iter().any(|&s| s.abs() > OUTLIER_THRESHOLD) {
|
||||
|
||||
+74
-4
@@ -1,9 +1,12 @@
|
||||
use core::f64;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::AddAssign;
|
||||
use std::ops::Div;
|
||||
|
||||
use serde::ser::SerializeStruct;
|
||||
use serde::Serializer;
|
||||
|
||||
use uom::num_traits;
|
||||
use uom::si;
|
||||
pub use uom::si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};
|
||||
pub use uom::si::ratio::ratio;
|
||||
@@ -173,10 +176,40 @@ macro_rules! quantity_fn {
|
||||
};
|
||||
}
|
||||
|
||||
quantity_fn!(mean, values, statistical::mean(&values));
|
||||
/// A max function that assumes no NaNs and at least one element
|
||||
pub fn max<V: PartialOrd>(values: impl IntoIterator<Item = V>) -> V {
|
||||
values
|
||||
.into_iter()
|
||||
.max_by(|a, b| a.partial_cmp(b).expect("No NaN values"))
|
||||
.expect("'max' requires at least one element")
|
||||
}
|
||||
|
||||
/// A min function that assumes no NaNs and at least one element
|
||||
pub fn min<V: PartialOrd>(values: impl IntoIterator<Item = V>) -> V {
|
||||
values
|
||||
.into_iter()
|
||||
.min_by(|a, b| a.partial_cmp(b).expect("No NaN values"))
|
||||
.expect("'min' requires at least one element")
|
||||
}
|
||||
|
||||
/// A mean function that assumes at least one element
|
||||
pub fn mean<Q, P>(values: impl IntoIterator<Item = Q>) -> Q
|
||||
where
|
||||
Q: AddAssign + num_traits::Zero + Div<Ratio, Output = P>,
|
||||
P: Into<Q>,
|
||||
{
|
||||
let mut sum = Q::zero();
|
||||
let mut count = 0;
|
||||
for value in values {
|
||||
sum += value;
|
||||
count += 1;
|
||||
}
|
||||
|
||||
let count = Ratio::new::<ratio>(count as f64);
|
||||
(sum / count).into()
|
||||
}
|
||||
|
||||
quantity_fn!(median, values, statistical::median(&values));
|
||||
quantity_fn!(min, values, crate::util::min_max::min(&values));
|
||||
quantity_fn!(max, values, crate::util::min_max::max(&values));
|
||||
quantity_fn!(standard_deviation, values, {
|
||||
let mean_value = statistical::mean(&values);
|
||||
statistical::standard_deviation(&values, Some(mean_value))
|
||||
@@ -187,6 +220,15 @@ pub fn modified_zscores<Q: UnsafeRawValue>(values: &[Q]) -> Vec<f64> {
|
||||
crate::outlier_detection::modified_zscores(&values)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_max() {
|
||||
assert_eq!(1.0, max([1.0]));
|
||||
assert_eq!(-1.0, max([-1.0]));
|
||||
assert_eq!(-1.0, max([-2.0, -1.0]));
|
||||
assert_eq!(1.0, max([-1.0, 1.0]));
|
||||
assert_eq!(1.0, max([-1.0, 1.0, 0.0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time() {
|
||||
let time = Time::new::<millisecond>(123.4);
|
||||
@@ -234,7 +276,7 @@ fn test_mean() {
|
||||
Time::new::<millisecond>(123.4),
|
||||
Time::new::<millisecond>(234.5),
|
||||
];
|
||||
let result = mean(&values);
|
||||
let result = mean(values.into_iter());
|
||||
assert_eq!(result.format(TimeUnit::MilliSecond), "178.9 ms");
|
||||
}
|
||||
|
||||
@@ -271,3 +313,31 @@ fn test_format_duration_unit_with_unit() {
|
||||
let out = Time::new::<second>(1.3).format(TimeUnit::MicroSecond);
|
||||
assert_eq!("1300000.0 µs", out);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn statistics() {
|
||||
let values = vec![
|
||||
Time::new::<second>(1.0),
|
||||
Time::new::<second>(2.0),
|
||||
Time::new::<second>(3.0),
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
mean(values.iter().copied()).format(TimeUnit::Second),
|
||||
"2.000 s"
|
||||
);
|
||||
assert_eq!(median(&values).format(TimeUnit::Second), "2.000 s");
|
||||
assert_eq!(min(&values).format(TimeUnit::Second), "1.000 s");
|
||||
assert_eq!(max(&values).format(TimeUnit::Second), "3.000 s");
|
||||
assert_eq!(
|
||||
standard_deviation(&values).format(TimeUnit::Second),
|
||||
"1.000 s"
|
||||
);
|
||||
|
||||
let values = vec![
|
||||
Information::new::<byte>(1.0),
|
||||
Information::new::<byte>(2.0),
|
||||
Information::new::<byte>(3.0),
|
||||
];
|
||||
mean(values.iter().copied()).format(InformationUnit::Byte);
|
||||
}
|
||||
Reference in new issue
Block a user