Files
hyperfine/src/quantity/mod.rs
T

273 lines
7.9 KiB
Rust
Raw Normal View History

2026-10-05 14:19:34 +02:00
use core::f64;
use std::marker::PhantomData;
2026-10-05 14:19:34 +02:00
use serde::ser::SerializeStruct;
use serde::Serializer;
2026-10-05 14:21:00 +02:00
use uom::si;
pub use uom::si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};
2026-10-05 14:19:34 +02:00
pub use uom::si::ratio::ratio;
pub use uom::si::time::{hour, microsecond, millisecond, minute, nanosecond, second};
2026-10-05 14:20:12 +02:00
pub use si::f64::{Information, Ratio, Time};
2026-10-05 11:46:49 +02:00
pub use units::{InformationUnit, IsUnit, TimeUnit};
mod units;
2026-10-05 14:21:00 +02:00
pub trait Quantity {
type Unit;
fn zero() -> Self;
fn suitable_unit(&self) -> Self::Unit;
fn format_with_precision(&self, unit: Self::Unit, precision: usize) -> String;
fn format(&self, unit: Self::Unit) -> String;
fn format_auto(&self) -> String;
fn format_value(&self, unit: Self::Unit) -> String;
}
impl Quantity for Time {
type Unit = TimeUnit;
2026-10-05 14:19:34 +02:00
fn zero() -> Time {
Time::new::<second>(0.0)
2026-10-05 11:46:49 +02:00
}
2026-10-05 14:19:47 +02:00
fn suitable_unit(&self) -> TimeUnit {
if *self < Time::new::<millisecond>(1.0) {
TimeUnit::MicroSecond
} else if *self < Time::new::<second>(1.0) {
TimeUnit::MilliSecond
} else {
TimeUnit::Second
}
2026-10-05 11:46:49 +02:00
}
2026-10-05 14:20:48 +02:00
/// Format the time duration in the given unit with the given precision.
fn format_with_precision(&self, u: TimeUnit, precision: usize) -> String {
2026-10-05 14:19:59 +02:00
u.format(*self, precision)
}
2026-10-05 14:20:48 +02:00
/// Format the time duration in the given unit.
fn format(&self, unit: TimeUnit) -> String {
let value = self.format_with_precision(unit, unit.preferred_precision());
format!("{} {}", value, unit.short_name())
2026-10-05 11:46:49 +02:00
}
2026-10-05 14:21:00 +02:00
/// Format the given time duration. The unit will be determined automatically.
fn format_auto(&self) -> String {
let unit = self.suitable_unit();
let value = self.format(unit);
format!("{} {}", value, unit.short_name())
}
2026-10-05 14:20:48 +02:00
/// Like `format`, but without displaying the unit.
fn format_value(&self, unit: TimeUnit) -> String {
self.format_with_precision(unit, unit.preferred_precision())
}
2026-10-05 11:46:49 +02:00
}
pub const fn const_time_from_seconds(value: f64) -> Time {
// Quantity::new in uom is not yet const: https://docs.rs/uom/0.36.0/uom/si/struct.Quantity.html
Time {
dimension: PhantomData,
units: PhantomData,
value,
}
}
2026-10-05 14:21:00 +02:00
impl Quantity for Information {
type Unit = InformationUnit;
2026-10-05 14:19:34 +02:00
fn zero() -> Information {
2026-10-05 14:21:00 +02:00
Information::new::<byte>(0.0)
2026-10-05 11:46:49 +02:00
}
2026-10-05 14:21:00 +02:00
fn suitable_unit(&self) -> InformationUnit {
if *self < Information::new::<kibibyte>(1.0) {
InformationUnit::Byte
} else {
InformationUnit::KibiByte
}
}
2026-10-05 14:19:34 +02:00
2026-10-05 14:21:00 +02:00
/// Format the information in the given unit with the given precision.
fn format_with_precision(&self, u: InformationUnit, precision: usize) -> String {
u.format(*self, precision)
}
/// Format the information in the given unit.
fn format(&self, unit: InformationUnit) -> String {
let value = self.format_with_precision(unit, unit.preferred_precision());
format!("{} {}", value, unit.short_name())
}
/// Format the given information. The unit will be determined automatically.
fn format_auto(&self) -> String {
let unit = self.suitable_unit();
let value = self.format(unit);
format!("{} {}", value, unit.short_name())
}
/// Like `format`, but without displaying the unit.
fn format_value(&self, unit: InformationUnit) -> String {
self.format_with_precision(unit, unit.preferred_precision())
}
}
2026-10-05 14:20:12 +02:00
pub trait UnsafeRawValue {
fn unsafe_raw_value(&self) -> f64;
fn unsafe_from_raw_value(value: f64) -> Self;
}
impl UnsafeRawValue for Time {
fn unsafe_raw_value(&self) -> f64 {
2026-10-05 14:20:23 +02:00
self.get::<second>()
2026-10-05 14:20:12 +02:00
}
fn unsafe_from_raw_value(value: f64) -> Self {
Time::new::<second>(value)
}
}
impl UnsafeRawValue for Information {
fn unsafe_raw_value(&self) -> f64 {
2026-10-05 14:20:23 +02:00
self.get::<byte>()
2026-10-05 14:20:12 +02:00
}
fn unsafe_from_raw_value(value: f64) -> Self {
Information::new::<byte>(value)
}
}
2026-10-05 14:19:34 +02:00
pub fn serialize_time<S>(t: &Time, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = s.serialize_struct("Time", 3)?;
state.serialize_field("value", &t.value)?;
state.serialize_field("unit", "second")?;
state.end()
}
pub fn serialize_information<S>(i: &Information, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = s.serialize_struct("Information", 3)?;
state.serialize_field("value", &i.value)?;
state.serialize_field("unit", "byte")?;
state.end()
}
macro_rules! quantity_fn {
($name:ident, $unwrapped_values:ident, $body:expr) => {
2026-10-05 14:20:12 +02:00
pub fn $name<Q: UnsafeRawValue>(values: &[Q]) -> Q {
let $unwrapped_values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();
2026-10-05 14:19:34 +02:00
let result_value = $body;
2026-10-05 14:20:12 +02:00
Q::unsafe_from_raw_value(result_value)
2026-10-05 14:19:34 +02:00
}
};
}
quantity_fn!(mean, values, statistical::mean(&values));
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))
});
2026-10-05 14:20:12 +02:00
pub fn modified_zscores<Q: UnsafeRawValue>(values: &[Q]) -> Vec<f64> {
let values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();
crate::outlier_detection::modified_zscores(&values)
}
#[test]
fn test_time() {
let time = Time::new::<millisecond>(123.4);
2026-10-05 14:20:23 +02:00
assert_eq!(time.get::<millisecond>(), 123.4);
2026-10-05 14:20:23 +02:00
let time_s = time.get::<second>();
approx::assert_relative_eq!(time_s, 0.1234);
2026-10-05 14:20:23 +02:00
let time_us = time.get::<microsecond>();
approx::assert_relative_eq!(time_us, 123400.0);
}
#[test]
fn test_information() {
2026-10-05 14:20:36 +02:00
use uom::si::information::pebibyte;
2026-10-05 14:20:12 +02:00
let information = Information::new::<kibibyte>(8.);
2026-10-05 14:20:23 +02:00
assert_eq!(information.get::<byte>(), 8192.);
2026-10-05 14:20:23 +02:00
let information_kib = information.get::<kibibyte>();
2026-10-05 14:20:12 +02:00
assert_eq!(information_kib, 8.);
2026-10-05 14:20:36 +02:00
let largest_exactly_representable = Information::new::<byte>(9_007_199_254_740_992.);
assert_eq!(largest_exactly_representable.get::<pebibyte>(), 8.);
}
#[test]
2026-10-05 14:19:34 +02:00
fn test_format() {
let time = Time::new::<millisecond>(123.4);
2026-10-05 14:20:48 +02:00
assert_eq!(time.format(TimeUnit::Second), "0.123 s");
assert_eq!(time.format(TimeUnit::MilliSecond), "123.4 ms");
assert_eq!(time.format(TimeUnit::MicroSecond), "123400.0 µs");
2026-10-05 14:19:34 +02:00
2026-10-05 14:20:12 +02:00
let peak_memory_usage = Information::new::<kibibyte>(8.);
2026-10-05 14:21:00 +02:00
assert_eq!(peak_memory_usage.format(InformationUnit::Byte), "8192 B");
assert_eq!(
peak_memory_usage.format(InformationUnit::KibiByte),
"8.0 KiB"
);
2026-10-05 11:46:49 +02:00
}
2026-10-05 14:19:34 +02:00
#[test]
fn test_mean() {
let values = vec![
Time::new::<millisecond>(123.4),
Time::new::<millisecond>(234.5),
];
let result = mean(&values);
2026-10-05 14:20:48 +02:00
assert_eq!(result.format(TimeUnit::MilliSecond), "178.9 ms");
2026-10-05 14:19:34 +02:00
}
#[test]
2026-10-05 14:19:47 +02:00
fn test_suiteable_unit() {
assert_eq!(Time::new::<second>(1.3).suitable_unit(), TimeUnit::Second);
assert_eq!(Time::new::<second>(1.0).suitable_unit(), TimeUnit::Second);
2026-10-05 11:46:49 +02:00
assert_eq!(
2026-10-05 14:19:47 +02:00
Time::new::<second>(0.999).suitable_unit(),
TimeUnit::MilliSecond
2026-10-05 11:46:49 +02:00
);
assert_eq!(
2026-10-05 14:19:47 +02:00
Time::new::<second>(0.0005).suitable_unit(),
TimeUnit::MicroSecond
2026-10-05 11:46:49 +02:00
);
assert_eq!(
2026-10-05 14:19:47 +02:00
Time::new::<second>(0.).suitable_unit(),
TimeUnit::MicroSecond
2026-10-05 11:46:49 +02:00
);
assert_eq!(
2026-10-05 14:19:47 +02:00
Time::new::<second>(1000.0).suitable_unit(),
TimeUnit::Second
2026-10-05 11:46:49 +02:00
);
}
#[test]
fn test_format_duration_unit_with_unit() {
2026-10-05 14:20:48 +02:00
let out = Time::new::<second>(1.3).format(TimeUnit::Second);
2026-10-05 14:19:47 +02:00
assert_eq!("1.300 s", out);
2026-10-05 14:20:48 +02:00
let out = Time::new::<second>(1.3).format(TimeUnit::MilliSecond);
2026-10-05 11:46:49 +02:00
assert_eq!("1300.0 ms", out);
2026-10-05 14:20:48 +02:00
let out = Time::new::<second>(1.3).format(TimeUnit::MicroSecond);
2026-10-05 14:19:47 +02:00
assert_eq!("1300000.0 µs", out);
}