ALib C++ Framework
by
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 #"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//==================================================================================================
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, quantity of
56 /// samples, minimum, and maximum values.
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 Start();
63 }
64
65 /// Returns the time span between the current system time and the internal start value.
66 /// In addition, this value is added to the sum of sample times and the sample counter is
67 /// increased by one. Lastly, the internal reference value is set to now. Therefore, a
68 /// subsequent call to this function would measure the time span from this call to this
69 /// subsequent call (if the internal start time value was not set differently meanwhile).
70 ///
71 /// @return The time difference between the current system time and the internal
72 /// reference value.
73 Ticks::Duration Sample() {
74 Ticks::Duration sample= startTime.Age();
75 sum+= sample;
76 if( min > sample ) min= sample;
77 if( max < sample ) max= sample;
78 ++cntSamples;
80 return sample;
81 }
82
83 /// Returns the number of calls to #"Sample" since this instance was created or #".Reset" was
84 /// invoked.
85 /// @return The number of samples.
86 int GetSampleCnt() const { return cntSamples; }
87
88 /// Returns the cumulated time of all samples taken since this instance was created or
89 /// cleared.
90 ///
91 /// @return The cumulated measured time.
92 Ticks::Duration GetCumulated() const { return sum; }
93
94 /// Returns the average time of all samples since this instance was created or reset.
95 /// If no measurement was performed, the result value will be set to \c 0.
96 ///
97 /// @return The cumulated measured time.
98 Ticks::Duration GetAverage() const {
99 return cntSamples== 0 ? Ticks::Duration()
100 : ( sum / int64_t(cntSamples) );
101 }
102
103 /// Returns the minimum duration of all samples since this instance was created or reset.
104 /// If no measurement was performed, the value evaluates to the minmum value storable
105 /// in type \b Ticks::Duration.
106 ///
107 /// @return The minimum measured duration.
108 Ticks::Duration GetMinimum() const { return min; }
109
110 /// Returns the maximum duration of all samples since this instance was created or reset.
111 /// If no measurement was performed, the value evaluates to the maximum value storable
112 /// in type \b Ticks::Duration.
113 ///
114 /// @return The maximum measured duration.
115 Ticks::Duration GetMaximum() const { return max; }
116};
117
118} // namespace alib[::time]
119
120/// Type alias in namespace \b alib.
122
123} // namespace [alib]
#define ALIB_EXPORT
Definition alib.inl:562
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:86
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:73
Ticks::Duration GetCumulated() const
Definition stopwatch.inl:92
Ticks::Duration GetMinimum() const
Ticks::Duration GetAverage() const
Definition stopwatch.inl:98
time::StopWatch StopWatch
Type alias in namespace alib.