AceTime  0.1
Date and time classes for Arduino that supports the TZ DAtabase, and a system clock synchronized from an NTP server or an RTC chip.
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_TIME_TIMING_STATS_H
26 #define ACE_TIME_TIMING_STATS_H
27 
28 #include <stdint.h>
29 
30 class Print;
31 
32 namespace ace_time {
33 namespace common {
34 
38 class TimingStats {
39  public:
41  TimingStats(): mCounter(0) {
42  reset();
43  }
44 
45  void reset() {
46  mExpDecayAvg = 0;
47  mMin = UINT16_MAX;
48  mMax = 0;
49  mSum = 0;
50  mCount = 0;
51  }
52 
53  uint16_t getMax() const { return mMax; }
54 
55  uint16_t getMin() const { return mMin; }
56 
57  uint16_t getAvg() const { return (mCount > 0) ? mSum / mCount : 0; }
58 
60  uint16_t getExpDecayAvg() const { return mExpDecayAvg; }
61 
63  uint16_t getCount() const { return mCount; }
64 
70  uint16_t getCounter() const { return mCounter; }
71 
72  void update(uint16_t duration) {
73  mCount++;
74  mCounter++;
75  mSum += duration;
76  if (duration < mMin) {
77  mMin = duration;
78  }
79  if (duration > mMax) {
80  mMax = duration;
81  }
82  mExpDecayAvg = (mExpDecayAvg + duration) / 2;
83  }
84 
85  private:
86  uint16_t mExpDecayAvg;
87  uint16_t mMin;
88  uint16_t mMax;
89  uint32_t mSum;
90  uint16_t mCount;
91  uint16_t mCounter;
92 };
93 
94 }
95 }
96 
97 #endif
uint16_t getCount() const
Number of times update() was called since last reset.
Definition: TimingStats.h:63
uint16_t getCounter() const
Number of times update() was called from the beginning of time.
Definition: TimingStats.h:70
Helper class to collect timing statistics such as min, max, average.
Definition: TimingStats.h:38
uint16_t getExpDecayAvg() const
An exponential decay average.
Definition: TimingStats.h:60