Skip to content
Myr

Restore original quantity trait and comments

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 a8e56ef · Browse files · 3 files+15 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+15 added−28 removed

  • other:+15 added−28 removed

3 files+15 added−28 removed

modified:src/benchmark/mod.rs-1 removed
440 unmodified lines
441 }442443 self.run_cleanup_command(self.command.get_parameters().iter().cloned(), output_policy)?;444445 // Keep the existing export representation: seconds and integer byte counts.445 Ok(BenchmarkResult {446 command: self.command.get_name(),447 command_with_unused_parameters: self.command.get_name_with_unused_parameters(),448 mean: t_mean.get::<second>(),
33 unmodified lines
modified:src/util/statistics.rs+15 added-23 removed
59 unmodified lines
60 values[len / 2]61 }62}6364/// This is just sad, but after several hours trying to figure out how to write64/// Access a quantity in its base unit for statistical operations.65pub trait RawValue {66 fn raw_value(&self) -> f64;67 fn from_raw_value(value: f64) -> Self;65/// a generic `standard_deviation` function using `uom` quantities, I gave up.66pub trait UnsafeRawValue {67 fn unsafe_raw_value(&self) -> f64;68 fn unsafe_from_raw_value(value: f64) -> Self;69}7070impl RawValue for Time {71 fn raw_value(&self) -> f64 {71impl UnsafeRawValue for Time {72 fn unsafe_raw_value(&self) -> f64 {73 self.get::<second>()74 }7575 fn from_raw_value(value: f64) -> Self {76 fn unsafe_from_raw_value(value: f64) -> Self {77 Time::new::<second>(value)78 }79}8080impl RawValue for Information {81 fn raw_value(&self) -> f64 {81impl UnsafeRawValue for Information {82 fn unsafe_raw_value(&self) -> f64 {83 self.get::<byte>()84 }8585 fn from_raw_value(value: f64) -> Self {86 fn unsafe_from_raw_value(value: f64) -> Self {87 Information::new::<byte>(value)88 }89}9090impl RawValue for f64 {91 fn raw_value(&self) -> f64 {92 *self93 }94 fn from_raw_value(value: f64) -> Self {95 value96 }97}9891/// Sample standard deviation, using an already computed mean.100pub fn standard_deviation<Q: RawValue>(values: &[Q], mean: Q) -> Q {101 let mean = mean.raw_value();92pub fn standard_deviation<Q: UnsafeRawValue>(values: &[Q], mean: Q) -> Q {93 let mean = mean.unsafe_raw_value();94 assert!(95 values.len() > 1,96 "standard deviation requires at least two measurements"97 );98 let sum = values99 .iter()108 .map(|value| (value.raw_value() - mean) * (value.raw_value() - mean))100 .map(|value| (value.unsafe_raw_value() - mean) * (value.unsafe_raw_value() - mean))101 .fold(0.0, |sum, deviation| sum + deviation);102 assert!(sum >= 0.0, "invalid sum of squared deviations");103104 // Preserve the operation order of statistical 1.0: dividing by n - 1105 // before taking the square root also preserves exported floating-point values.114 Q::from_raw_value((sum / (values.len() as f64 - 1.0)).sqrt())106 Q::unsafe_from_raw_value((sum / (values.len() as f64 - 1.0)).sqrt())107}108109#[cfg(test)]110mod tests {
85 unmodified lines
modified:src/quantity.rs-4 removed
1//! Typed quantities for benchmark measurements.2//!3//! Both time and information use `f64` storage. Whole-byte counts are exact up to 8 PiB.41use std::marker::PhantomData;23pub use uom::num_traits::Zero;4pub use uom::si::f64::{Information, Ratio, Time};
41 unmodified lines