CVB++ 15.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::make_unique<RateCounter>(windowSize);
58 }
59
61
68 {
69 return std::make_unique<RateCounter>(windowSize, mode);
70 }
71
73
77 : stopWatch_(Cvb::StopWatchMode::MultiCPU)
78 {
79 }
80
82
86 explicit 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
106 RateCounter(const RateCounter& other) = delete;
107 RateCounter& operator=(const RateCounter & other) = delete;
108 RateCounter(RateCounter&& other) = default; // moving std::deque may throw
109 RateCounter& operator=(RateCounter&& other) = default; // moving std::deque may throw
110 ~RateCounter() = default;
111
112
113
114
116
120 StopWatchMode Mode() const noexcept
121 {
122 return stopWatch_.Mode();
123 }
124
126
130 int WindowSize() const noexcept
131 {
132 return windowSize_;
133 }
134
135
137
141 void SetWindowSize(int windowSize)
142 {
143 if (windowSize_ == windowSize)
144 return;
145
146 if (windowSize < MinimumWindowSize || windowSize > MaximumWindowSize)
147 throw std::out_of_range("Window size out of range.");
148
149 windowSize_ = windowSize;
150
151 while (static_cast<int>(measurements_.size()) > windowSize_)
152 measurements_.pop_front();
153 }
154
155
156
157
159
164 {
165 double sum = std::accumulate(measurements_.begin(), measurements_.end(), 0.0);
166 if (!measurements_.size())
168
169 auto result = std::round(sum / static_cast<double>(measurements_.size()));
170 return std::chrono::milliseconds(static_cast<std::chrono::milliseconds::rep>(result));
171 }
172
173
175
179 double Rate() const noexcept
180 {
181 if (measurements_.size() < 2)
183
184 auto averageTimeSpan = AverageTimeSpan();
185 return 1000.0 / static_cast<double>(averageTimeSpan.count());
186 }
187
188
190
194 int NumSteps() const noexcept
195 {
196 return numSteps_;
197 }
198
200
203 void Reset() noexcept
204 {
205 measurements_.clear();
207 numSteps_ = 0;
208 }
209
211
214 void Step() noexcept
215 {
216 if (std::isnan(lastReading_))
217 {
218 lastReading_ = 0.0;
219 stopWatch_.Start();
220 }
221 else
222 {
223 auto now = static_cast<double>(stopWatch_.TimeSpan().count());
224 measurements_.push_back(now - lastReading_);
225 lastReading_ = now;
226 ++numSteps_;
227
228 while (static_cast<int>(measurements_.size()) > windowSize_)
229 measurements_.pop_front();
230 }
231 }
232
233
234
235
236
237
238 private:
239
240 StopWatch stopWatch_;
241
242 int windowSize_ = DefaultWindowSize;
243
244 std::deque<double> measurements_;
245
246 double lastReading_ = std::numeric_limits<double>::quiet_NaN();
247
248 int numSteps_ = 0;
249};
250
251
252}
253
254
255CVB_END_INLINE_NS
256
257}
258
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:163
void SetWindowSize(int windowSize)
Sets the currently used averaging window size.
Definition: rate_counter.hpp:141
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:203
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:179
int NumSteps() const noexcept
Number of steps called since construction or since last reset.
Definition: rate_counter.hpp:194
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:214
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:130
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:120
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:136
std::chrono::milliseconds TimeSpan() const
Gets the time, that has elapsed since start (or since the construction of the object,...
Definition: stop_watch.hpp:147
StopWatchMode Mode() const noexcept
Mode for which the stop watch was created.
Definition: stop_watch.hpp:160
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)