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