Class MovingAverage
Moving average calculator. Calculates average value of all given items Safe to use in cross-thread environment
public class MovingAverage
- Inheritance
-
objectMovingAverage
- Extension Methods
Constructors
MovingAverage(TimeSpan)
Creates an empty set that keeps the values added within the last interval.
public MovingAverage(TimeSpan interval)
Parameters
intervalTimeSpanThe length of the time window; older values are dropped on the next call of any method.
Methods
GetAverage()
Returns an average of all items
public double GetAverage()
Returns
- double
The average of the values.
Remarks
Covers the values added within the interval; 0 when there are none.
GetHistory()
Returns an array of all records in the interval
public (long Timestamp, int Value)[] GetHistory()
Returns
- (long Timestamp, int Value)[]
The records with their timestamps.
Remarks
The records are in the order they were added; Timestamp is a System.Diagnostics.Stopwatch.GetTimestamp() value
(ticks of System.Diagnostics.Stopwatch.Frequency), not a date. The array is a copy.
GetMax()
Get maximum value in the set
public int GetMax()
Returns
- int
The largest value.
Remarks
Covers the values added within the interval; 0 when there are none.
GetSum()
Returns the sum of the values added within the interval; 0 when there are none.
public int GetSum()
Returns
- int
The sum of the values.
Exceptions
- OverflowException
The sum exceeds the range of int.
Update(int)
Adds value to the set with current timestamp
public void Update(int value)
Parameters
valueintThe value to add.