Consolidate min and max in the statistics module
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 6940578 · Browse files · 4 files·+43 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+43 added−33 removed
- other:+43 added−33 removed
Provenance
- Commit
305e89d- Tree
8a1ad7e- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
6940578
4 files changed+43 added−33 removed
modified:src/benchmark/mod.rs+3 added-4 removed
17 unmodified lines18 use crate::output::progress_bar::get_progress_bar;19 use crate::output::warnings::{OutlierWarningOptions, Warnings};20 use crate::parameter::ParameterNameAndValue;21 use crate::util::exit_code::extract_exit_code;22-use crate::util::min_max::{max, min};23-use crate::util::statistics::{mean, median, standard_deviation};22+use crate::util::statistics::{max, mean, median, min, standard_deviation};23 use crate::util::units::Second;24 use benchmark_result::BenchmarkResult;25 use measurement::Measurement;26 315 unmodified lines342 } else {343 None344 };345 let t_median = median(×_real);347- let t_min = min(×_real);348- let t_max = max(×_real);346+ let t_min = min(times_real.iter().copied());347+ let t_max = max(times_real.iter().copied());348 349 let user_mean = mean(×_user);350 let system_mean = mean(×_system);351 122 unmodified linesdeleted:src/util/min_max.rs-28 removed
1-/// A max function for f64's without NaNs2-pub fn max(vals: &[f64]) -> f64 {3- *vals4- .iter()5- .max_by(|a, b| a.partial_cmp(b).unwrap())6- .unwrap()7-}8-9-/// A min function for f64's without NaNs10-pub fn min(vals: &[f64]) -> f64 {11- *vals12- .iter()13- .min_by(|a, b| a.partial_cmp(b).unwrap())14- .unwrap()15-}16-17-#[test]18-fn test_max() {19- let assert_float_eq = |a: f64, b: f64| {20- assert!((a - b).abs() < f64::EPSILON);21- };22-23- assert_float_eq(1.0, max(&[1.0]));24- assert_float_eq(-1.0, max(&[-1.0]));25- assert_float_eq(-1.0, max(&[-2.0, -1.0]));26- assert_float_eq(1.0, max(&[-1.0, 1.0]));27- assert_float_eq(1.0, max(&[-1.0, 1.0, 0.0]));28-}modified:src/util/mod.rs-1 removed
1 pub mod exit_code;2-pub mod min_max;2 pub mod number;3 pub mod randomized_environment_offset;4 pub mod statistics;5 pub mod units;modified:src/util/statistics.rs+40 added
1 //! Statistics for non-empty samples of finite measurements.2 3+/// A min function that assumes no NaNs and at least one element4+pub fn min<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {5+ values6+ .into_iter()7+ .min_by(|a, b| a.partial_cmp(b).expect("No NaN values"))8+ .expect("'min' requires at least one element")9+}10+11+/// A max function that assumes no NaNs and at least one element12+pub fn max<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {13+ values14+ .into_iter()15+ .max_by(|a, b| a.partial_cmp(b).expect("No NaN values"))16+ .expect("'max' requires at least one element")17+}18+19 /// Arithmetic mean, summing measurements in their original order.20 pub fn mean(values: &[f64]) -> f64 {21 values.iter().fold(0.0, |sum, value| sum + value) / values.len() as f6422 }37 unmodified lines60 use super::*;61 use approx::assert_relative_eq;62 63 #[test]64+ fn test_min() {65+ assert_eq!(1.0, min([1.0]));66+ assert_eq!(-1.0, min([-1.0]));67+ assert_eq!(-2.0, min([-2.0, -1.0]));68+ assert_eq!(-1.0, min([-1.0, 1.0]));69+ assert_eq!(-1.0, min([1.0, -1.0, 0.0]));70+71+ let values = vec![1024.0, 2.0, 3.0];72+ assert_eq!(*min(&values), 2.0);73+ }74+75+ #[test]76+ fn test_max() {77+ assert_eq!(1.0, max([1.0]));78+ assert_eq!(-1.0, max([-1.0]));79+ assert_eq!(-1.0, max([-2.0, -1.0]));80+ assert_eq!(1.0, max([-1.0, 1.0]));81+ assert_eq!(1.0, max([-1.0, 1.0, 0.0]));82+83+ let values = vec![1.0, 2048.0, 3.0];84+ assert_eq!(*max(&values), 2048.0);85+ }86+87+ #[test]88 fn test_mean() {89 assert_eq!(1.0, mean(&[1.0]));90 assert_relative_eq!(2.0, mean(&[1.0, 3.0]));91 22 unmodified lines