AceCommon  1.5.2
Arduino library for low-level common functions and features with no external dependencies
TimingStats.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_COMMON_TIMING_STATS_H
26 #define ACE_COMMON_TIMING_STATS_H
27 
28 #include <stdint.h>
29 
30 namespace ace_common {
31 
36 class TimingStats {
37  public:
39  TimingStats(): mCounter(0) {
40  reset();
41  }
42 
43  // Use default copy-constructor and assignment operator. Doxygen docs added
44  // to keep it quiet.
45 
47  TimingStats(const TimingStats&) = default;
48 
50  TimingStats& operator=(const TimingStats&) = default;
51 
56  void reset() {
57  mExpDecayAvg = 0;
58  mMin = UINT16_MAX;
59  mMax = 0;
60  mSum = 0;
61  mCount = 0;
62  }
63 
65  uint16_t getMax() const { return mMax; }
66 
68  uint16_t getMin() const { return mMin; }
69 
74  uint16_t getAvg() const { return (mCount > 0) ? mSum / mCount : 0; }
75 
77  uint16_t getExpDecayAvg() const { return mExpDecayAvg; }
78 
80  uint16_t getCount() const { return mCount; }
81 
87  uint16_t getCounter() const { return mCounter; }
88 
90  void update(uint16_t duration) {
91  mSum += duration;
92  if (duration < mMin) {
93  mMin = duration;
94  }
95  if (duration > mMax) {
96  mMax = duration;
97  }
98 
99  if (mCount == 0) {
100  mExpDecayAvg = duration;
101  } else {
102  // Use a slightly convoluted averaging algorithm to prevent uint16_t
103  // overflow and to preserve accuracy in the least significant bit.
104  mExpDecayAvg = (mExpDecayAvg/2) + (duration/2)
105  + ((mExpDecayAvg & 0x1) & (duration & 0x1));
106  }
107 
108  mCount++;
109  mCounter++;
110  }
111 
112  private:
113  uint16_t mExpDecayAvg;
114  uint16_t mMin;
115  uint16_t mMax;
116  uint32_t mSum;
117  uint16_t mCount;
118  uint16_t mCounter;
119 };
120 
121 }
122 
123 #endif
Helper class to collect timing statistics such as min, max, average, and exponential-decay average.
Definition: TimingStats.h:36
TimingStats & operator=(const TimingStats &)=default
Default assignment operator.
uint16_t getMax() const
Return the maximum since the last reset().
Definition: TimingStats.h:65
uint16_t getMin() const
Return the minium since the last reset().
Definition: TimingStats.h:68
uint16_t getCount() const
Number of times update() was called since last reset().
Definition: TimingStats.h:80
TimingStats(const TimingStats &)=default
Default copy constructor.
void update(uint16_t duration)
Add the given duration (often in milliseconds) to the statistics.
Definition: TimingStats.h:90
TimingStats()
Constructor.
Definition: TimingStats.h:39
uint16_t getCounter() const
Number of times update() was called from the beginning of time.
Definition: TimingStats.h:87
void reset()
Reset the object to its initial state, except mCounter which is never reset.
Definition: TimingStats.h:56
uint16_t getAvg() const
Return the average since the last reset().
Definition: TimingStats.h:74
uint16_t getExpDecayAvg() const
An exponential decay average since the last reset().
Definition: TimingStats.h:77