pub struct Timer { /* private fields */ }Expand description
High-level timer for measuring function execution.
Wraps the low-level cycle counter with calibration and conversion to nanoseconds.
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn with_cycles_per_ns(cycles_per_ns: f64) -> Self
pub fn with_cycles_per_ns(cycles_per_ns: f64) -> Self
Create a timer with a known cycles-per-nanosecond value.
Useful for testing or when calibration has already been done.
Sourcepub fn cycles_per_ns(&self) -> f64
pub fn cycles_per_ns(&self) -> f64
Get the calibrated cycles per nanosecond.
Sourcepub fn resolution_ns(&self) -> f64
pub fn resolution_ns(&self) -> f64
Get the estimated timer resolution in nanoseconds.
On x86_64, this is typically ~0.3-1ns (CPU frequency TSC). On aarch64, this varies by SoC: ~1ns on ARMv8.6+ (Graviton4), ~40ns on Apple Silicon/Ampere Altra, ~18ns on Raspberry Pi 4.
Sourcepub fn suggested_iterations(&self, target_resolution_ns: f64) -> usize
pub fn suggested_iterations(&self, target_resolution_ns: f64) -> usize
Suggest minimum iterations per sample based on timer resolution.
For operations faster than the timer resolution, batching multiple iterations per measurement ensures reliable timing data.
§Arguments
target_resolution_ns- Desired effective resolution (default: 10ns)
§Returns
Recommended iterations per sample (minimum 1).
Sourcepub fn measure_cycles<F, T>(&self, f: F) -> MeasurementResultwhere
F: FnOnce() -> T,
pub fn measure_cycles<F, T>(&self, f: F) -> MeasurementResultwhere
F: FnOnce() -> T,
Measure the execution time of a function in cycles.
Uses black_box to prevent optimization of the measured function.
§Errors
This implementation always succeeds (register-based timers cannot fail),
but returns Result for API consistency with PMU-based timers.
Sourcepub fn measure_ns<F, T>(&self, f: F) -> Result<f64, MeasurementError>where
F: FnOnce() -> T,
pub fn measure_ns<F, T>(&self, f: F) -> Result<f64, MeasurementError>where
F: FnOnce() -> T,
Measure the execution time of a function in nanoseconds.
§Errors
This implementation always succeeds (register-based timers cannot fail),
but returns Result for API consistency with PMU-based timers.
Sourcepub fn cycles_to_ns(&self, cycles: u64) -> f64
pub fn cycles_to_ns(&self, cycles: u64) -> f64
Convert cycles to nanoseconds using calibrated ratio.
Sourcepub fn measure_batched_cycles<F, T>(
&self,
iterations: usize,
f: F,
) -> MeasurementResultwhere
F: FnMut() -> T,
pub fn measure_batched_cycles<F, T>(
&self,
iterations: usize,
f: F,
) -> MeasurementResultwhere
F: FnMut() -> T,
Measure batched iterations and return per-iteration cycles.
Runs the function iterations times and returns the average
cycles per iteration. This is useful when timer resolution is
too coarse for single-iteration measurements.
§Errors
This implementation always succeeds (register-based timers cannot fail),
but returns Result for API consistency with PMU-based timers.
Sourcepub fn measure_batched_ns<F, T>(
&self,
iterations: usize,
f: F,
) -> Result<f64, MeasurementError>where
F: FnMut() -> T,
pub fn measure_batched_ns<F, T>(
&self,
iterations: usize,
f: F,
) -> Result<f64, MeasurementError>where
F: FnMut() -> T,
Measure batched iterations and return per-iteration nanoseconds.
§Errors
This implementation always succeeds (register-based timers cannot fail),
but returns Result for API consistency with PMU-based timers.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Timer
impl RefUnwindSafe for Timer
impl Send for Timer
impl Sync for Timer
impl Unpin for Timer
impl UnwindSafe for Timer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.