Struct DiscreteRateCounter

Source
pub struct DiscreteRateCounter { /* private fields */ }
Expand description

A very basic non-rolling update counter. It counts n updates, calculates, and then resets (where n is the sample rate), which means that it takes at least n updates to react to a change in rate appropriately.

Generally, the RollingRateCounter is a better instrument, but it has to recalculate its measured rate every time it is queried whereas the DiscreteRateCounter only recalculates every n cycles.

§Usage

Call .update() every time your system starts a new update/cycle; for instance, an FPS counter would call this at the beginning of every frame. The sample rate (set with set_sample_rate() and in the first argument to new()) governs how many .update() calls are required before a meaningful result is produced.

You can also use .update_immut() for this. Since DiscreteRateCounter is small and easily copyable, this is negligibly less efficient.

Implementations§

Source§

impl DiscreteRateCounter

Source

pub fn new(samples: u64) -> Self

Create a new DiscreteRateCounter which calculates the update rate every samples cycles. Until that many cycles occur, rate() will return a useless value, typically 0.0.

If this isn’t acceptable, one strategy is to start the samples value at 0 and keep ramping it up until it reaches your target samples value; however, the data near the beginning will not be useful.

Source

pub fn rate_age_cycles(&self) -> u64

Return the number of cycles since the rate was last recalculated.

Source

pub fn rate_age_duration(&self) -> Duration

Return the amount of time since the rate was last recalculated. This requires examining the system clock and is thus relatively expensive.

Trait Implementations§

Source§

impl Clone for DiscreteRateCounter

Source§

fn clone(&self) -> DiscreteRateCounter

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DiscreteRateCounter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for DiscreteRateCounter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl RateCounter for DiscreteRateCounter

Source§

fn samples(&self) -> u64

Return the current number of samples the UpdateRateCounter is measuring.
Source§

fn set_samples(&mut self, samples: u64)

Set the number of updates which the UpdateRateCounter considers. Read more
Source§

fn update(&mut self)

Updates the struct in place, but requires a mutable binding. Call either this OR update_immut() at the beginning of each cycle of the periodic activity being measured.
Source§

fn rate(&self) -> f64

Return the last calculated rate of operation, in Hertz (updates per second).
Source§

impl RateCounterImmut for DiscreteRateCounter

Source§

fn update_immut(self) -> Self

Consumes the struct and returns an updated version. Call this at the beginning of each cycle of the periodic activity being measured.

§Examples
use update_rate::DiscreteRateCounter;
use update_rate::{RateCounter, RateCounterImmut};
let c = DiscreteRateCounter::new(5);
for i in 1..101 {
    let c = c.update_immut();
    if i % 10 == 0 {println!("Rate: {}", c.rate())}
    // Do work here
}
Source§

impl Copy for DiscreteRateCounter

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.