Struct apollo_framework::average::Average [−][src]
pub struct Average { /* fields omitted */ }
Computes a sliding average of a series of values.
This is intended to record performance measurements and to keep track of the sliding average as well as the total number of recorded values.
Note that this class overflows gracefully.
Example
let avg = Average::new(); avg.add(10); avg.add(20); avg.add(30); assert_eq!(avg.avg(), 20); assert_eq!(avg.count(), 3);
Implementations
impl Average
[src]
impl Average
[src]pub fn add(&self, value: i32)
[src]
pub fn add(&self, value: i32)
[src]Adds another value to be added to the average calculation.
Internally we simply update the global u64 counter to keep track of the total recorded values. Additionally, we have another u64 which is split into two i32 fields. One of these is used to keep the actual count of the sliding average and another is used to store the sum of the values.
Whenever we recorded 100 values or the sum counter might overflow, we divide both values by two and add the new values. This yields a sliding average which is fit for our purposes.
As the main task is to store the average duration of a task in microseconds, the i32 sum field shouldn’t overflow under normal conditions.
We perform this trickery (splitting a single field into two) so that this algorithm is completely lock and wait free, as we only utilize atomic load and store operations. This guarantees correctness while ensuring maximal performance.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Average
impl Send for Average
impl Sync for Average
impl Unpin for Average
impl UnwindSafe for Average
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn to_owned(&self) -> T
[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
[src]
pub fn clone_into(&self, target: &mut T)
[src]🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more