Unit safe statistics functions

This commit is contained in:
David Peter authored and David Peter committed 2026-10-05 16:00:35 +02:00
1 parent f342e1f5de
commit 3a2c8ab177
3 files changed
+88 -40

No files matched your search

+11 -32
View File
@@ -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 {
+3 -4
View File
@@ -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) {