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