mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 08:50:19 +00:00
Group units, formatting, and statistics under quantity
This commit is contained in:
1 parent
bef66df7e4
commit
aa14b8c0d9
20 files changed
+266
-265
No files matched your search
@@ -0,0 +1,56 @@
|
||||
//! This module contains common units.
|
||||
|
||||
/// Supported time units
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TimeUnit {
|
||||
Second,
|
||||
MilliSecond,
|
||||
MicroSecond,
|
||||
Minute,
|
||||
Hour,
|
||||
}
|
||||
|
||||
impl TimeUnit {
|
||||
/// The abbreviation of the TimeUnit.
|
||||
pub fn short_name(self) -> String {
|
||||
match self {
|
||||
TimeUnit::Second => String::from("s"),
|
||||
TimeUnit::MilliSecond => String::from("ms"),
|
||||
TimeUnit::MicroSecond => String::from("µs"),
|
||||
TimeUnit::Minute => String::from("min"),
|
||||
TimeUnit::Hour => String::from("h"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the value in seconds formatted for the TimeUnit.
|
||||
pub fn format(self, value: f64) -> String {
|
||||
match self {
|
||||
TimeUnit::Second => format!("{value:.3}"),
|
||||
TimeUnit::MilliSecond => format!("{:.1}", value * 1e3),
|
||||
TimeUnit::MicroSecond => format!("{:.1}", value * 1e6),
|
||||
TimeUnit::Minute => format!("{:.1}", value / 60.0),
|
||||
TimeUnit::Hour => format!("{:.1}", value / 3600.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unit_short_name() {
|
||||
assert_eq!("s", TimeUnit::Second.short_name());
|
||||
assert_eq!("ms", TimeUnit::MilliSecond.short_name());
|
||||
assert_eq!("µs", TimeUnit::MicroSecond.short_name());
|
||||
assert_eq!("min", TimeUnit::Minute.short_name());
|
||||
assert_eq!("h", TimeUnit::Hour.short_name());
|
||||
}
|
||||
|
||||
// Note - the values are rounded when formatted.
|
||||
#[test]
|
||||
fn test_unit_format() {
|
||||
let value: f64 = 123.456789;
|
||||
assert_eq!("123.457", TimeUnit::Second.format(value));
|
||||
assert_eq!("123456.8", TimeUnit::MilliSecond.format(value));
|
||||
|
||||
assert_eq!("1234.6", TimeUnit::MicroSecond.format(0.00123456));
|
||||
assert_eq!("2.1", TimeUnit::Minute.format(value));
|
||||
assert_eq!("0.0", TimeUnit::Hour.format(value));
|
||||
}
|
||||
Reference in new issue
Block a user