pub struct FrameStats<const N: usize = 120> { /* private fields */ }Expand description
A fixed-size ring buffer of recent per-frame durations, plus the min/max/average/fps readouts derived from it.
N bounds memory and how far back the window looks; the default, 120 samples (about two
seconds at 60fps), is a reasonable window for a live overlay. Every reducer
(avg, min, max, fps) is computed
on demand from the current window rather than maintained incrementally, since a live overlay
only calls them once per rendered frame, not once per sample.
Readouts are Duration, not a pre-chosen unit: a caller formats with whatever precision it
needs (as_millis(), as_secs_f32(), …) rather than being handed milliseconds it then has
to convert back if it wants something else. current is the single most
recent sample, unsmoothed: pair it with the windowed min/max for
a “current, min, max” readout, and samples for a frame-time graph (feed it,
converted to milliseconds, straight into
Sparkline).
Implementations§
Source§impl<const N: usize> FrameStats<N>
impl<const N: usize> FrameStats<N>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
An empty window; every readout is Duration::ZERO (or 0.0 for fps)
until the first record.
Sourcepub fn record(&mut self, delta: Duration)
pub fn record(&mut self, delta: Duration)
Records one frame’s wall-clock duration.
Call once per rendered frame with Frame::delta. A no-op if
N is 0 (frame count still advances) rather than panicking: a zero-capacity window is
a degenerate but harmless configuration, not a caller error worth crashing over.
Sourcepub const fn frame_count(&self) -> u64
pub const fn frame_count(&self) -> u64
Total frames ever recorded via record, uncapped by N.
Sourcepub fn samples(&self) -> impl ExactSizeIterator<Item = Duration> + '_
pub fn samples(&self) -> impl ExactSizeIterator<Item = Duration> + '_
Recorded frame durations, oldest first (so the most recent frame is last): the order a sparkline/histogram wants so new data enters on the right.
Sourcepub const fn current(&self) -> Duration
pub const fn current(&self) -> Duration
The most recently recorded frame’s duration, unsmoothed. Duration::ZERO before the
first record.
Sourcepub fn avg(&self) -> Duration
pub fn avg(&self) -> Duration
The average frame duration over the current window. Duration::ZERO before the first
record.
Sourcepub fn min(&self) -> Duration
pub fn min(&self) -> Duration
The fastest (shortest) frame in the current window. Duration::ZERO before the first
record.
Sourcepub fn max(&self) -> Duration
pub fn max(&self) -> Duration
The slowest (longest) frame in the current window. Duration::ZERO before the first
record.
Trait Implementations§
Source§impl<const N: usize> Clone for FrameStats<N>
impl<const N: usize> Clone for FrameStats<N>
Source§fn clone(&self) -> FrameStats<N>
fn clone(&self) -> FrameStats<N>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more