ALib C++ Framework
by
Library Version: 2605 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 the \aliblong. It does not belong to an \alibmod and is
4/// included in any \alibbuild.
5///
6/// Copyright 2013-2026 A-Worx GmbH, Germany.
7/// Published under #"mainpage_license".
8//==================================================================================================
9ALIB_EXPORT namespace alib { namespace time {
10
11//==================================================================================================
12/// This class encapsulates a system-dependent timer value of type #"Ticks" and provides
13/// some simple interface for measuring multiple time spans and providing their sum, average,
14/// minimum and maximum.
15/// @see
16/// For this class, a #"alibtools_debug_helpers_gdb;pretty printer" for the
17/// GNU debugger is provided.
18//==================================================================================================
19class StopWatch {
20 protected:
21
22 /// The current start time.
24
25 /// The number of samples performed.
26 int cntSamples =0;
27
28 /// The sum of the samples times.
29 Ticks::Duration sum;
30
31 /// The minimum duration probed.
32 Ticks::Duration min;
33
34 /// The maximum duration probed.
35 Ticks::Duration max;
36
37
38 public:
39 /// Creates a started StopWatch.
41 : startTime()
42 , sum() {}
43
44 public:
45 /// Provides access to the internal start time.
46 /// @return The start time
48
49 /// Sets the start time to now.
50 /// This affects both, the reference value for the calculation of this StopWatch's age in
51 /// subsequent calls, as well as subsequent sample time spans.
52 void Start() { startTime= Ticks::Now(); }
53
54 /// Sets the internal value to current system time and clears existing sum, quantity of
55 /// samples, minimum, and maximum values.
56 void Reset() {
57 sum= Ticks::Duration::FromNanoseconds(0);
58 cntSamples= 0;
59 min= (std::numeric_limits<Ticks::Duration>::max)();
60 max= (std::numeric_limits<Ticks::Duration>::min)();
61 Start();
62 }
63
64 /// Returns the time span between the current system time and the internal start value.
65 /// In addition, this value is added to the sum of sample times and the sample counter is
66 /// increased by one. Lastly, the internal reference value is set to now. Therefore, a
67 /// subsequent call to this function would measure the time span from this call to this
68 /// subsequent call (if the internal start time value was not set differently meanwhile).
69 ///
70 /// @return The time difference between the current system time and the internal
71 /// reference value.
72 Ticks::Duration Sample() {
73 Ticks::Duration sample= startTime.Age();
74 sum+= sample;
75 if( min > sample ) min= sample;
76 if( max < sample ) max= sample;
77 ++cntSamples;
79 return sample;
80 }
81
82 /// Returns the number of calls to #"Sample" since this instance was created or #".Reset" was
83 /// invoked.
84 /// @return The number of samples.
85 int GetSampleCnt() const { return cntSamples; }
86
87 /// Returns the cumulated time of all samples taken since this instance was created or
88 /// cleared.
89 ///
90 /// @return The cumulated measured time.
91 Ticks::Duration GetCumulated() const { return sum; }
92
93 /// Returns the average time of all samples since this instance was created or reset.
94 /// If no measurement was performed, the result value will be set to \c 0.
95 ///
96 /// @return The cumulated measured time.
97 Ticks::Duration GetAverage() const {
98 return cntSamples== 0 ? Ticks::Duration()
99 : ( sum / int64_t(cntSamples) );
100 }
101
102 /// Returns the minimum duration of all samples since this instance was created or reset.
103 /// If no measurement was performed, the value evaluates to the minimum value storable
104 /// in type #"%^Ticks::Duration".
105 ///
106 /// @return The minimum measured duration.
107 Ticks::Duration GetMinimum() const { return min; }
108
109 /// Returns the maximum duration of all samples since this instance was created or reset.
110 /// If no measurement was performed, the value evaluates to the maximum value storable
111 /// in type #"%^Ticks::Duration".
112 ///
113 /// @return The maximum measured duration.
114 Ticks::Duration GetMaximum() const { return max; }
115};
116
117} // namespace alib[::time]
118
119/// Type alias in namespace #"%alib".
121
122} // namespace [alib]
#define ALIB_EXPORT
int cntSamples
The number of samples performed.
Definition stopwatch.hpp:26
Ticks::Duration sum
The sum of the samples times.
Definition stopwatch.hpp:29
Ticks startTime
The current start time.
Definition stopwatch.hpp:23
int GetSampleCnt() const
Definition stopwatch.hpp:85
Ticks::Duration GetMaximum() const
Ticks::Duration max
The maximum duration probed.
Definition stopwatch.hpp:35
StopWatch()
Creates a started StopWatch.
Definition stopwatch.hpp:40
Ticks::Duration min
The minimum duration probed.
Definition stopwatch.hpp:32
Ticks::Duration Sample()
Definition stopwatch.hpp:72
Ticks::Duration GetCumulated() const
Definition stopwatch.hpp:91
Ticks::Duration GetMinimum() const
Ticks::Duration GetAverage() const
Definition stopwatch.hpp:97
Definition alox.cpp:14
time::StopWatch StopWatch
Type alias in namespace #"%alib".