CVB++ 14.0
rate_counter.hpp
1#pragma once
2
3#include <memory>
4#include <deque>
5#include <numeric>
6#include <iostream>
7
8#include "../global.hpp"
9#include "utilities.hpp"
10#include "stop_watch.hpp"
11
12
13namespace Cvb
14{
15
16CVB_BEGIN_INLINE_NS
17
18namespace Utilities
19{
20
22
25{
26 public:
27
28
29
31 static constexpr int MinimumWindowSize = 1;
32
34 static constexpr int DefaultWindowSize = 25;
35
37 static constexpr int MaximumWindowSize = 65535;
38
40
45 {
47 }
48
50
55 static std::unique_ptr<RateCounter> Create(int windowSize)
56 {
57 return std::unique_ptr<RateCounter>(new RateCounter(windowSize));
58 }
59
61
68 {
69 return std::unique_ptr<RateCounter>(new RateCounter(windowSize, mode));
70 }
71
73
77 : stopWatch_(Cvb::StopWatchMode::MultiCPU)
78 {
79 }
80
82
86 RateCounter(int windowSize)
87 : stopWatch_(Cvb::StopWatchMode::MultiCPU)
88 , windowSize_(windowSize)
89 {
90
91 }
92
94
99 RateCounter(int windowSize, StopWatchMode mode)
100 : stopWatch_(mode)
101 , windowSize_(windowSize)
102 {
103
104 }
105
107
111 RateCounter(RateCounter&& other) = default;
112
113
114 RateCounter& operator=(RateCounter&& other) noexcept = default;
115
116
117
118
119
121
125 StopWatchMode Mode() const noexcept
126 {
127 return stopWatch_.Mode();
128 }
129
131
135 int WindowSize() const noexcept
136 {
137 return windowSize_;
138 }
139
140
142
146 void SetWindowSize(int windowSize)
147 {
148 if (windowSize_ == windowSize)
149 return;
150
151 if (windowSize < MinimumWindowSize || windowSize > MaximumWindowSize)
152 throw std::out_of_range("Window size out of range.");
153
154 windowSize_ = windowSize;
155
156 while (static_cast<int>(measurements_.size()) > windowSize_)
157 measurements_.pop_front();
158 }
159
160
161
162
164
169 {
170 double sum = std::accumulate(measurements_.begin(), measurements_.end(), 0.0);
171 if (!measurements_.size())
173
174 auto result = std::round(sum / static_cast<double>(measurements_.size()));
175 return std::chrono::milliseconds(static_cast<std::chrono::milliseconds::rep>(result));
176 }
177
178
180
184 double Rate() const noexcept
185 {
186 if (measurements_.size() < 2)
188
189 auto averageTimeSpan = AverageTimeSpan();
190 return 1000.0 / static_cast<double>(averageTimeSpan.count());
191 }
192
193
195
199 int NumSteps() const noexcept
200 {
201 return numSteps_;
202 }
203
205
208 void Reset() noexcept
209 {
210 measurements_.clear();
212 numSteps_ = 0;
213 }
214
216
219 void Step() noexcept
220 {
221 if (std::isnan(lastReading_))
222 {
223 lastReading_ = 0.0;
224 stopWatch_.Start();
225 }
226 else
227 {
228 auto now = static_cast<double>(stopWatch_.TimeSpan().count());
229 measurements_.push_back(now - lastReading_);
230 lastReading_ = now;
231 ++numSteps_;
232
233 while (static_cast<int>(measurements_.size()) > windowSize_)
234 measurements_.pop_front();
235 }
236 }
237
238
239
240
241
242
243 private:
244
245 StopWatch stopWatch_;
246
247 int windowSize_ = DefaultWindowSize;
248
249 std::deque<double> measurements_;
250
251 double lastReading_ = std::numeric_limits<double>::quiet_NaN();
252
253 int numSteps_ = 0;
254};
255
256
257}
258
259
260CVB_END_INLINE_NS
261
262}
263
Frame rate measurement counter with selectable averaging window.
Definition: rate_counter.hpp:25
RateCounter(int windowSize, StopWatchMode mode)
Constructor for a rate counter object.
Definition: rate_counter.hpp:99
static constexpr int MinimumWindowSize
Minimal selectable averaging window size.Trying to select a smaller window will result in an exceptio...
Definition: rate_counter.hpp:31
std::chrono::milliseconds AverageTimeSpan() const noexcept
Average time span between two steps or since the creation of the counter or the most recent reset.
Definition: rate_counter.hpp:168
void SetWindowSize(int windowSize)
Sets the currently used averaging window size.
Definition: rate_counter.hpp:146
static std::unique_ptr< RateCounter > Create(int windowSize)
Creates a frame counter object for multi CPU.
Definition: rate_counter.hpp:55
void Reset() noexcept
Erase all measurements so far.
Definition: rate_counter.hpp:208
static std::unique_ptr< RateCounter > Create()
Creates a frame counter object for multi CPU with default averaging window (25).
Definition: rate_counter.hpp:44
double Rate() const noexcept
Returns the rate(1 / s) at which the steps occurred.
Definition: rate_counter.hpp:184
int NumSteps() const noexcept
Number of steps called since construction or since last reset.
Definition: rate_counter.hpp:199
static constexpr int MaximumWindowSize
Maximum selectable averaging window size. Trying to select a larger window will result in an exceptio...
Definition: rate_counter.hpp:37
void Step() noexcept
Reading step (first reading step will not yield a measurement result, but define the starting point).
Definition: rate_counter.hpp:219
RateCounter(RateCounter &&other)=default
Move constructor.
RateCounter()
Constructor for a rate counter object for multi-CPU architectures with default averaging window size ...
Definition: rate_counter.hpp:76
int WindowSize() const noexcept
Gets the currently used averaging window size.
Definition: rate_counter.hpp:135
static std::unique_ptr< RateCounter > Create(int windowSize, StopWatchMode mode)
Creates a frame counter object.
Definition: rate_counter.hpp:67
static constexpr int DefaultWindowSize
Default averaging window size.
Definition: rate_counter.hpp:34
StopWatchMode Mode() const noexcept
The stop watch mode used internally.
Definition: rate_counter.hpp:125
RateCounter(int windowSize)
Constructor for a rate counter object for multi-CPU architectures.
Definition: rate_counter.hpp:86
Speed measurement object.
Definition: stop_watch.hpp:47
void Start()
Start (or re-start) the stopwatch.
Definition: stop_watch.hpp:139
std::chrono::milliseconds TimeSpan() const
Gets the time, that has elapsed since start (or since the construction of the object,...
Definition: stop_watch.hpp:150
StopWatchMode Mode() const noexcept
Mode for which the stop watch was created.
Definition: stop_watch.hpp:163
StopWatchMode
Mode at which the StopWatch should work.
Definition: utilities.hpp:31
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
T quiet_NaN(T... args)