ALib C++ Library
Library Version: 2511 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 /// Creates a started StopWatch.
42 : startTime()
43 , sum() {}
44
45 public:
46 /// Provides access to the internal start time.
47 /// @return The start time
49
50 /// Sets the start time to now.
51 /// This affects both, the reference value for the calculation of this StopWatch's age in
52 /// subsequent calls, as well as subsequent sample time spans.
53 void Start() { startTime= Ticks::Now(); }
54
55 /// Sets the internal value to current system time and clears existing sum and quantity of
56 /// samples.
57 void Reset() {
58 sum= Ticks::Duration::FromNanoseconds(0);
59 cntSamples= 0;
60 min= (std::numeric_limits<Ticks::Duration>::max)();
61 max= (std::numeric_limits<Ticks::Duration>::min)();
62
63 Start();
64 }
65
66 /// Returns the time span between the current system time and the internal start value.
67 /// In addition this value is added to the sum of sample times and the sample counter is
68 /// increased by one. Lastly the internal reference value is set to now. Therefore, a
69 /// subsequent call to this function would measure the time span from this call to this
70 /// subsequent call (if the internal start time value was not set differently meanwhile).
71 ///
72 /// @return The time difference between the current system time and the internal
73 /// reference value.
74 Ticks::Duration Sample() {
75 Ticks::Duration sample= startTime.Age();
76 sum+= sample;
77 if( min > sample ) min= sample;
78 if( max < sample ) max= sample;
79 ++cntSamples;
81
82 return sample;
83 }
84
85 /// Returns the number of calls to #Sample since this instance was created or #Reset was
86 /// invoked.
87 /// @return The number of samples.
88 int GetSampleCnt() const { return cntSamples; }
89
90 /// Returns the cumulated time of all samples taken since this instance was created or
91 /// cleared.
92 ///
93 /// @return The cumulated measured time.
94 Ticks::Duration GetCumulated() const { return sum; }
95
96 /// Returns the average time of all samples since this instance was created or reset.
97 /// If no measurement was performed, the result value will be set to \c 0.
98 ///
99 /// @return The cumulated measured time.
100 Ticks::Duration GetAverage() const {
101 return cntSamples== 0 ? Ticks::Duration()
102 : ( sum / int64_t(cntSamples) );
103 }
104
105 /// Returns the minimum duration of all samples since this instance was created or reset.
106 /// If no measurement was performed, the value evaluates to the minmum value storable
107 /// in type \b Ticks::Duration.
108 ///
109 /// @return The minimum measured duration.
110 Ticks::Duration GetMinimum() const { return min; }
111
112 /// Returns the maximum duration of all samples since this instance was created or reset.
113 /// If no measurement was performed, the value evaluates to the maximum value storable
114 /// in type \b Ticks::Duration.
115 ///
116 /// @return The maximum measured duration.
117 Ticks::Duration GetMaximum() const { return max; }
118};
119
120} // namespace alib[::time]
121
122/// Type alias in namespace \b alib.
124
125} // namespace [alib]
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
Definition stopwatch.inl:88
Ticks::Duration GetMaximum() const
Ticks::Duration max
The maximum duration probed.
Definition stopwatch.inl:36
StopWatch()
Creates a started StopWatch.
Definition stopwatch.inl:41
Ticks::Duration min
The minimum duration probed.
Definition stopwatch.inl:33
Ticks::Duration Sample()
Definition stopwatch.inl:74
Ticks::Duration GetCumulated() const
Definition stopwatch.inl:94
Ticks::Duration GetMinimum() const
Ticks::Duration GetAverage() const
#define ALIB_EXPORT
Definition alib.inl:497
time::StopWatch StopWatch
Type alias in namespace alib.