AceCommon  1.1.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 TIMING_STATS_TIMING_STATS_H
26 #define TIMING_STATS_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  mExpDecayAvg = (mCount > 0) ? (mExpDecayAvg + duration) / 2 : duration;
100 
101  mCount++;
102  mCounter++;
103  }
104 
105  private:
106  uint16_t mExpDecayAvg;
107  uint16_t mMin;
108  uint16_t mMax;
109  uint32_t mSum;
110  uint16_t mCount;
111  uint16_t mCounter;
112 };
113 
114 }
115 
116 #endif
ace_common::TimingStats::getExpDecayAvg
uint16_t getExpDecayAvg() const
An exponential decay average since the last reset().
Definition: TimingStats.h:77
ace_common::TimingStats::getCounter
uint16_t getCounter() const
Number of times update() was called from the beginning of time.
Definition: TimingStats.h:87
ace_common::TimingStats::reset
void reset()
Reset the object to its initial state, except mCounter which is never reset.
Definition: TimingStats.h:56
ace_common::TimingStats::getMax
uint16_t getMax() const
Return the maximum since the last reset().
Definition: TimingStats.h:65
ace_common::TimingStats::getMin
uint16_t getMin() const
Return the minium since the last reset().
Definition: TimingStats.h:68
ace_common::TimingStats::getAvg
uint16_t getAvg() const
Return the average since the last reset().
Definition: TimingStats.h:74
ace_common::TimingStats::TimingStats
TimingStats()
Constructor.
Definition: TimingStats.h:39
ace_common::TimingStats::operator=
TimingStats & operator=(const TimingStats &)=default
Default assignment operator.
ace_common::TimingStats
Helper class to collect timing statistics such as min, max, average, and exponential-decay average.
Definition: TimingStats.h:36
ace_common::TimingStats::getCount
uint16_t getCount() const
Number of times update() was called since last reset().
Definition: TimingStats.h:80
ace_common::TimingStats::update
void update(uint16_t duration)
Add the given duration (often in milliseconds) to the statistics.
Definition: TimingStats.h:90