ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
stopwatch.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_time of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_TIME_STOPWATCH
9#define HPP_ALIB_TIME_STOPWATCH 1
10
11#if !defined (HPP_ALIB_TIME_TICKS)
12 #include "alib/time/ticks.hpp"
13#endif
14
15namespace alib { namespace time {
16
17/** ************************************************************************************************
18 * This class encapsulates a system dependent timer value of type \alib{time;Ticks} and provides
19 * some simple interface for measuring multiple time spans and providing their sum, average, minimum
20 * and maximum.
21 **************************************************************************************************/
23{
24 protected:
25
26 /** The current start time. */
28
29 /** The number of samples performed. */
30 int cntSamples =0;
31
32 /** The sum of the samples times. */
33 Ticks::Duration sum;
34
35 /** The minimum duration probed. */
36 Ticks::Duration min;
37
38 /** The maximum duration probed. */
39 Ticks::Duration max;
40
41
42 public:
43 /** ****************************************************************************************
44 * Creates a started StopWatch.
45 ******************************************************************************************/
47 : startTime()
48 , sum()
49 {}
50
51 public:
52 /** ****************************************************************************************
53 * Provides access to the internal start time.
54 * @return The start time
55 ******************************************************************************************/
57 {
58 return startTime;
59 }
60
61 /** ****************************************************************************************
62 * Sets the start time to now.
63 * This affects both, the reference value for the calculation of this StopWatch's age in
64 * subsequent calls, as well as subsequent sample time spans.
65 ******************************************************************************************/
66 void Start()
67 {
69 }
70
71 /** ****************************************************************************************
72 * Sets the internal value to current system time and clears existing sum and quantity of
73 * samples.
74 ******************************************************************************************/
75 void Reset()
76 {
77 sum= Ticks::Duration::FromNanoseconds(0);
78 cntSamples= 0;
79 min= (std::numeric_limits<Ticks::Duration>::max)();
80 max= (std::numeric_limits<Ticks::Duration>::min)();
81
82 Start();
83 }
84
85 /** ****************************************************************************************
86 * Returns the time span between the current system time and the internal start value.
87 * In addition this value is added to the sum of sample times and the sample counter is
88 * increased by one. Lastly the internal reference value is set to now. Therefore, a
89 * subsequent call to this function would measure the time span from this call to this
90 * subsequent call (if the internal start time value was not set differently meanwhile).
91 *
92 * @return The time difference between the current system time and the internal
93 * reference value.
94 ******************************************************************************************/
95 Ticks::Duration Sample()
96 {
97 Ticks::Duration sample= startTime.Age();
98 sum+= sample;
99 if( min > sample ) min= sample;
100 if( max < sample ) max= sample;
101 ++cntSamples;
103
104 return sample;
105 }
106
107 /** ****************************************************************************************
108 * Returns the number of calls to #Sample since this instance was created or #Reset was
109 * invoked.
110 * @return The number of samples.
111 ******************************************************************************************/
112 int GetSampleCnt() const
113 {
114 return cntSamples;
115 }
116
117 /** ****************************************************************************************
118 * Returns the cumulated time of all samples taken since this instance was created or
119 * cleared.
120 *
121 * @return The cumulated measured time.
122 ******************************************************************************************/
123 Ticks::Duration GetCumulated() const
124 {
125 return sum;
126 }
127
128 /** ****************************************************************************************
129 * Returns the average time of all samples since this instance was created or reset.
130 * If no measurement was performed, the result value will be set to \c 0.
131 *
132 * @return The cumulated measured time.
133 ******************************************************************************************/
134 Ticks::Duration GetAverage() const
135 {
136 return cntSamples== 0 ? Ticks::Duration()
137 : ( sum / static_cast<int64_t>(cntSamples) );
138 }
139
140 /** ****************************************************************************************
141 * Returns the minimum duration of all samples since this instance was created or reset.
142 * If no measurement was performed, the value evaluates to the minmum value storable
143 * in type \b Ticks::Duration.
144 *
145 * @return The minimum measured duration.
146 ******************************************************************************************/
147 Ticks::Duration GetMinimum() const
148 {
149 return min;
150 }
151
152 /** ****************************************************************************************
153 * Returns the maximum duration of all samples since this instance was created or reset.
154 * If no measurement was performed, the value evaluates to the maximum value storable
155 * in type \b Ticks::Duration.
156 *
157 * @return The maximum measured duration.
158 ******************************************************************************************/
159 Ticks::Duration GetMaximum() const
160 {
161 return max;
162 }
163};
164
165} // namespace alib[::time]
166
167/// Type alias in namespace \b alib.
169
170} // namespace [alib]
171
172#endif // HPP_ALIB_TIME_STOPWATCH
Ticks::Duration Sample()
Definition stopwatch.hpp:95
Ticks::Duration GetCumulated() const
int GetSampleCnt() const
Ticks::Duration GetAverage() const
Ticks::Duration sum
Definition stopwatch.hpp:33
Ticks::Duration GetMaximum() const
Ticks::Duration min
Definition stopwatch.hpp:36
Ticks::Duration max
Definition stopwatch.hpp:39
Ticks::Duration GetMinimum() const
Definition alib.cpp:57