1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qelapsedtimer.h"
41
42QT_BEGIN_NAMESPACE
43
44/*!
45 \class QElapsedTimer
46 \inmodule QtCore
47 \brief The QElapsedTimer class provides a fast way to calculate elapsed times.
48 \since 4.7
49
50 \reentrant
51 \ingroup tools
52
53 The QElapsedTimer class is usually used to quickly calculate how much
54 time has elapsed between two events. Its API is similar to that of QTime,
55 so code that was using that can be ported quickly to the new class.
56
57 However, unlike QTime, QElapsedTimer tries to use monotonic clocks if
58 possible. This means it's not possible to convert QElapsedTimer objects
59 to a human-readable time.
60
61 The typical use-case for the class is to determine how much time was
62 spent in a slow operation. The simplest example of such a case is for
63 debugging purposes, as in the following example:
64
65 \snippet qelapsedtimer/main.cpp 0
66
67 In this example, the timer is started by a call to start() and the
68 elapsed time is calculated by the elapsed() function.
69
70 The time elapsed can also be used to recalculate the time available for
71 another operation, after the first one is complete. This is useful when
72 the execution must complete within a certain time period, but several
73 steps are needed. The \tt{waitFor}-type functions in QIODevice and its
74 subclasses are good examples of such need. In that case, the code could
75 be as follows:
76
77 \snippet qelapsedtimer/main.cpp 1
78
79 Another use-case is to execute a certain operation for a specific
80 timeslice. For this, QElapsedTimer provides the hasExpired() convenience
81 function, which can be used to determine if a certain number of
82 milliseconds has already elapsed:
83
84 \snippet qelapsedtimer/main.cpp 2
85
86 It is often more convenient to use \l{QDeadlineTimer} in this case, which
87 counts towards a timeout in the future instead of tracking elapsed time.
88
89 \section1 Reference Clocks
90
91 QElapsedTimer will use the platform's monotonic reference clock in all
92 platforms that support it (see QElapsedTimer::isMonotonic()). This has
93 the added benefit that QElapsedTimer is immune to time adjustments, such
94 as the user correcting the time. Also unlike QTime, QElapsedTimer is
95 immune to changes in the timezone settings, such as daylight-saving
96 periods.
97
98 On the other hand, this means QElapsedTimer values can only be compared
99 with other values that use the same reference. This is especially true if
100 the time since the reference is extracted from the QElapsedTimer object
101 (QElapsedTimer::msecsSinceReference()) and serialised. These values
102 should never be exchanged across the network or saved to disk, since
103 there's no telling whether the computer node receiving the data is the
104 same as the one originating it or if it has rebooted since.
105
106 It is, however, possible to exchange the value with other processes
107 running on the same machine, provided that they also use the same
108 reference clock. QElapsedTimer will always use the same clock, so it's
109 safe to compare with the value coming from another process in the same
110 machine. If comparing to values produced by other APIs, you should check
111 that the clock used is the same as QElapsedTimer (see
112 QElapsedTimer::clockType()).
113
114 \section2 32-bit overflows
115
116 Some of the clocks used by QElapsedTimer have a limited range and may
117 overflow after hitting the upper limit (usually 32-bit). QElapsedTimer
118 deals with this overflow issue and presents a consistent timing. However,
119 when extracting the time since reference from QElapsedTimer, two
120 different processes in the same machine may have different understanding
121 of how much time has actually elapsed.
122
123 The information on which clocks types may overflow and how to remedy that
124 issue is documented along with the clock types.
125
126 \sa QTime, QTimer, QDeadlineTimer
127*/
128
129/*!
130 \enum QElapsedTimer::ClockType
131
132 This enum contains the different clock types that QElapsedTimer may use.
133
134 QElapsedTimer will always use the same clock type in a particular
135 machine, so this value will not change during the lifetime of a program.
136 It is provided so that QElapsedTimer can be used with other non-Qt
137 implementations, to guarantee that the same reference clock is being
138 used.
139
140 \value SystemTime The human-readable system time. This clock is not monotonic.
141 \value MonotonicClock The system's monotonic clock, usually found in Unix systems. This clock is monotonic and does not overflow.
142 \value TickCounter Not used anymore.
143 \value MachAbsoluteTime The Mach kernel's absolute time (\macos and iOS). This clock is monotonic and does not overflow.
144 \value PerformanceCounter The performance counter provided by Windows. This clock is monotonic and does not overflow.
145
146 \section2 SystemTime
147
148 The system time clock is purely the real time, expressed in milliseconds
149 since Jan 1, 1970 at 0:00 UTC. It's equivalent to the value returned by
150 the C and POSIX \tt{time} function, with the milliseconds added. This
151 clock type is currently only used on Unix systems that do not support
152 monotonic clocks (see below).
153
154 This is the only non-monotonic clock that QElapsedTimer may use.
155
156 \section2 MonotonicClock
157
158 This is the system's monotonic clock, expressed in milliseconds since an
159 arbitrary point in the past. This clock type is used on Unix systems
160 which support POSIX monotonic clocks (\tt{_POSIX_MONOTONIC_CLOCK}).
161
162 This clock does not overflow.
163
164 \section2 MachAbsoluteTime
165
166 This clock type is based on the absolute time presented by Mach kernels,
167 such as that found on \macos. This clock type is presented separately
168 from MonotonicClock since \macos and iOS are also Unix systems and may support
169 a POSIX monotonic clock with values differing from the Mach absolute
170 time.
171
172 This clock is monotonic and does not overflow.
173
174 \section2 PerformanceCounter
175
176 This clock uses the Windows functions \tt{QueryPerformanceCounter} and
177 \tt{QueryPerformanceFrequency} to access the system's performance counter.
178
179 This clock is monotonic and does not overflow.
180
181 \sa clockType(), isMonotonic()
182*/
183
184/*!
185 \fn QElapsedTimer::QElapsedTimer()
186 \since 5.4
187
188 Constructs an invalid QElapsedTimer. A timer becomes valid once it has been
189 started.
190
191 \sa isValid(), start()
192*/
193
194
195/*!
196 \fn bool QElapsedTimer::operator ==(const QElapsedTimer &other) const
197
198 Returns \c true if this object and \a other contain the same time.
199*/
200
201/*!
202 \fn bool QElapsedTimer::operator !=(const QElapsedTimer &other) const
203
204 Returns \c true if this object and \a other contain different times.
205*/
206
207static const qint64 invalidData = Q_INT64_C(0x8000000000000000);
208
209/*!
210 Marks this QElapsedTimer object as invalid.
211
212 An invalid object can be checked with isValid(). Calculations of timer
213 elapsed since invalid data are undefined and will likely produce bizarre
214 results.
215
216 \sa isValid(), start(), restart()
217*/
218void QElapsedTimer::invalidate() noexcept
219{
220 t1 = t2 = invalidData;
221}
222
223/*!
224 Returns \c false if the timer has never been started or invalidated by a
225 call to invalidate().
226
227 \sa invalidate(), start(), restart()
228*/
229bool QElapsedTimer::isValid() const noexcept
230{
231 return t1 != invalidData && t2 != invalidData;
232}
233
234/*!
235 Returns \c true if this QElapsedTimer has already expired by \a timeout
236 milliseconds (that is, more than \a timeout milliseconds have elapsed).
237 The value of \a timeout can be -1 to indicate that this timer does not
238 expire, in which case this function will always return false.
239
240 \sa elapsed(), QDeadlineTimer
241*/
242bool QElapsedTimer::hasExpired(qint64 timeout) const noexcept
243{
244 // if timeout is -1, quint64(timeout) is LLINT_MAX, so this will be
245 // considered as never expired
246 return quint64(elapsed()) > quint64(timeout);
247}
248
249QT_END_NAMESPACE
250

source code of qtbase/src/corelib/kernel/qelapsedtimer.cpp