Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qelapsedtimer.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
8
9/*!
10 \class QElapsedTimer
11 \inmodule QtCore
12 \brief The QElapsedTimer class provides a fast way to calculate elapsed times.
13 \since 4.7
14
15 \reentrant
16 \ingroup tools
17
18 \compares strong
19
20 The QElapsedTimer class is usually used to quickly calculate how much
21 time has elapsed between two events. Its API is similar to that of QTime,
22 so code that was using that can be ported quickly to the new class.
23
24 However, unlike QTime, QElapsedTimer tries to use monotonic clocks if
25 possible. This means it's not possible to convert QElapsedTimer objects
26 to a human-readable time.
27
28 The typical use-case for the class is to determine how much time was
29 spent in a slow operation. The simplest example of such a case is for
30 debugging purposes, as in the following example:
31
32 \snippet qelapsedtimer/main.cpp 0
33
34 In this example, the timer is started by a call to start() and the
35 elapsed time is calculated by the elapsed() function.
36
37 The time elapsed can also be used to recalculate the time available for
38 another operation, after the first one is complete. This is useful when
39 the execution must complete within a certain time period, but several
40 steps are needed. The \tt{waitFor}-type functions in QIODevice and its
41 subclasses are good examples of such need. In that case, the code could
42 be as follows:
43
44 \snippet qelapsedtimer/main.cpp 1
45
46 Another use-case is to execute a certain operation for a specific
47 timeslice. For this, QElapsedTimer provides the hasExpired() convenience
48 function, which can be used to determine if a certain number of
49 milliseconds has already elapsed:
50
51 \snippet qelapsedtimer/main.cpp 2
52
53 It is often more convenient to use \l{QDeadlineTimer} in this case, which
54 counts towards a timeout in the future instead of tracking elapsed time.
55
56 \section1 Reference Clocks
57
58 QElapsedTimer will use the platform's monotonic reference clock in all
59 platforms that support it (see QElapsedTimer::isMonotonic()). This has
60 the added benefit that QElapsedTimer is immune to time adjustments, such
61 as the user correcting the time. Also unlike QTime, QElapsedTimer is
62 immune to changes in the timezone settings, such as daylight-saving
63 periods.
64
65 On the other hand, this means QElapsedTimer values can only be compared
66 with other values that use the same reference. This is especially true if
67 the time since the reference is extracted from the QElapsedTimer object
68 (QElapsedTimer::msecsSinceReference()) and serialised. These values
69 should never be exchanged across the network or saved to disk, since
70 there's no telling whether the computer node receiving the data is the
71 same as the one originating it or if it has rebooted since.
72
73 It is, however, possible to exchange the value with other processes
74 running on the same machine, provided that they also use the same
75 reference clock. QElapsedTimer will always use the same clock, so it's
76 safe to compare with the value coming from another process in the same
77 machine. If comparing to values produced by other APIs, you should check
78 that the clock used is the same as QElapsedTimer (see
79 QElapsedTimer::clockType()).
80
81 \sa QTime, QChronoTimer, QDeadlineTimer
82*/
83
84/*!
85 \enum QElapsedTimer::ClockType
86
87 This enum contains the different clock types that QElapsedTimer may use.
88
89 QElapsedTimer will always use the same clock type in a particular
90 machine, so this value will not change during the lifetime of a program.
91 It is provided so that QElapsedTimer can be used with other non-Qt
92 implementations, to guarantee that the same reference clock is being
93 used.
94
95 \value SystemTime The human-readable system time. This clock is not monotonic.
96 \value MonotonicClock The system's monotonic clock, usually found in Unix systems.
97 This clock is monotonic.
98 \value TickCounter Not used anymore.
99 \value MachAbsoluteTime The Mach kernel's absolute time (\macos and iOS).
100 This clock is monotonic.
101 \value PerformanceCounter The performance counter provided by Windows.
102 This clock is monotonic.
103
104 \section2 SystemTime
105
106 The system time clock is purely the real time, expressed in milliseconds
107 since Jan 1, 1970 at 0:00 UTC. It's equivalent to the value returned by
108 the C and POSIX \tt{time} function, with the milliseconds added. This
109 clock type is currently only used on Unix systems that do not support
110 monotonic clocks (see below).
111
112 This is the only non-monotonic clock that QElapsedTimer may use.
113
114 \section2 MonotonicClock
115
116 This is the system's monotonic clock, expressed in milliseconds since an
117 arbitrary point in the past. This clock type is used on Unix systems
118 which support POSIX monotonic clocks (\tt{_POSIX_MONOTONIC_CLOCK}).
119
120 \section2 MachAbsoluteTime
121
122 This clock type is based on the absolute time presented by Mach kernels,
123 such as that found on \macos. This clock type is presented separately
124 from MonotonicClock since \macos and iOS are also Unix systems and may support
125 a POSIX monotonic clock with values differing from the Mach absolute
126 time.
127
128 This clock is monotonic.
129
130 \section2 PerformanceCounter
131
132 This clock uses the Windows functions \tt{QueryPerformanceCounter} and
133 \tt{QueryPerformanceFrequency} to access the system's performance counter.
134
135 This clock is monotonic.
136
137 \sa clockType(), isMonotonic()
138*/
139
140/*!
141 \fn QElapsedTimer::QElapsedTimer()
142 \since 5.4
143
144 Constructs an invalid QElapsedTimer. A timer becomes valid once it has been
145 started.
146
147 \sa isValid(), start()
148*/
149
150/*!
151 \fn bool QElapsedTimer::operator==(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
152
153 Returns \c true if \a lhs and \a rhs contain the same time, false otherwise.
154*/
155/*!
156 \fn bool QElapsedTimer::operator!=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
157
158 Returns \c true if \a lhs and \a rhs contain different times, false otherwise.
159*/
160/*!
161 \fn bool QElapsedTimer::operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
162
163 Returns \c true if \a lhs was started before \a rhs, false otherwise.
164
165 The returned value is undefined if one of the two parameters is invalid
166 and the other isn't. However, two invalid timers are equal and thus this
167 function will return false.
168*/
169
170/*!
171 \fn QElapsedTimer::clockType() noexcept
172
173 Returns the clock type that this QElapsedTimer implementation uses.
174
175 Since Qt 6.6, QElapsedTimer uses \c{std::chrono::steady_clock}, so the
176 clock type is always \l MonotonicClock.
177
178 \sa isMonotonic()
179*/
180
181QElapsedTimer::ClockType QElapsedTimer::clockType() noexcept
182{
183 // we use std::chrono::steady_clock
184 return MonotonicClock;
185}
186
187/*!
188 \fn QElapsedTimer::isMonotonic() noexcept
189
190 Returns \c true if this is a monotonic clock, false otherwise. See the
191 information on the different clock types to understand which ones are
192 monotonic.
193
194 Since Qt 6.6, QElapsedTimer uses \c{std::chrono::steady_clock}, so this
195 function now always returns true.
196
197 \sa clockType(), QElapsedTimer::ClockType
198*/
199bool QElapsedTimer::isMonotonic() noexcept
200{
201 // We trust std::chrono::steady_clock to be steady (monotonic); if the
202 // Standard Library is lying to us, users must complain to their vendor.
203 return true;
204}
205
206/*!
207 \typealias QElapsedTimer::Duration
208 Synonym for \c std::chrono::nanoseconds.
209*/
210
211/*!
212 \typealias QElapsedTimer::TimePoint
213 Synonym for \c {std::chrono::time_point<std::chrono::steady_clock, Duration>}.
214*/
215
216/*!
217 Starts this timer. Once started, a timer value can be checked with elapsed() or msecsSinceReference().
218
219 Normally, a timer is started just before a lengthy operation, such as:
220 \snippet qelapsedtimer/main.cpp 0
221
222 Also, starting a timer makes it valid again.
223
224 \sa restart(), invalidate(), elapsed()
225*/
226void QElapsedTimer::start() noexcept
227{
228 static_assert(sizeof(t1) == sizeof(Duration::rep));
229
230 // This assignment will work so long as TimePoint uses the same time
231 // duration or one of finer granularity than steady_clock::time_point. That
232 // means it will work until the first steady_clock using picoseconds.
233 TimePoint now = std::chrono::steady_clock::now();
234 t1 = now.time_since_epoch().count();
235 QT6_ONLY(t2 = 0);
236}
237
238/*!
239 Restarts the timer and returns the number of milliseconds elapsed since
240 the previous start.
241 This function is equivalent to obtaining the elapsed time with elapsed()
242 and then starting the timer again with start(), but it does so in one
243 single operation, avoiding the need to obtain the clock value twice.
244
245 Calling this function on a QElapsedTimer that is invalid
246 results in undefined behavior.
247
248 The following example illustrates how to use this function to calibrate a
249 parameter to a slow operation (for example, an iteration count) so that
250 this operation takes at least 250 milliseconds:
251
252 \snippet qelapsedtimer/main.cpp 3
253
254 \sa start(), invalidate(), elapsed(), isValid()
255*/
256qint64 QElapsedTimer::restart() noexcept
257{
258 QElapsedTimer old = *this;
259 start();
260 return old.msecsTo(*this);
261}
262
263/*!
264 \since 6.6
265
266 Returns a \c{std::chrono::nanoseconds} with the time since this QElapsedTimer was last
267 started.
268
269 Calling this function on a QElapsedTimer that is invalid
270 results in undefined behavior.
271
272 On platforms that do not provide nanosecond resolution, the value returned
273 will be the best estimate available.
274
275 \sa start(), restart(), hasExpired(), invalidate()
276*/
277auto QElapsedTimer::durationElapsed() const noexcept -> Duration
278{
279 TimePoint then{Duration(t1)};
280 return std::chrono::steady_clock::now() - then;
281}
282
283/*!
284 \since 4.8
285
286 Returns the number of nanoseconds since this QElapsedTimer was last
287 started.
288
289 Calling this function on a QElapsedTimer that is invalid
290 results in undefined behavior.
291
292 On platforms that do not provide nanosecond resolution, the value returned
293 will be the best estimate available.
294
295 \sa start(), restart(), hasExpired(), invalidate()
296*/
297qint64 QElapsedTimer::nsecsElapsed() const noexcept
298{
299 return durationElapsed().count();
300}
301
302/*!
303 Returns the number of milliseconds since this QElapsedTimer was last
304 started.
305
306 Calling this function on a QElapsedTimer that is invalid
307 results in undefined behavior.
308
309 \sa start(), restart(), hasExpired(), isValid(), invalidate()
310*/
311qint64 QElapsedTimer::elapsed() const noexcept
312{
313 using namespace std::chrono;
314 return duration_cast<milliseconds>(durationElapsed()).count();
315}
316
317/*!
318 Returns the number of milliseconds between last time this QElapsedTimer
319 object was started and its reference clock's start.
320
321 This number is usually arbitrary for all clocks except the
322 QElapsedTimer::SystemTime clock. For that clock type, this number is the
323 number of milliseconds since January 1st, 1970 at 0:00 UTC (that is, it
324 is the Unix time expressed in milliseconds).
325
326 On Linux, Windows and Apple platforms, this value is usually the time
327 since the system boot, though it usually does not include the time the
328 system has spent in sleep states.
329
330 \sa clockType(), elapsed()
331*/
332qint64 QElapsedTimer::msecsSinceReference() const noexcept
333{
334 using namespace std::chrono;
335 return duration_cast<milliseconds>(Duration(t1)).count();
336}
337
338/*!
339 \since 6.6
340
341 Returns the time difference between this QElapsedTimer and \a other as a
342 \c{std::chrono::nanoseconds}. If \a other was started before this object,
343 the returned value will be negative. If it was started later, the returned
344 value will be positive.
345
346 The return value is undefined if this object or \a other were invalidated.
347
348 \sa secsTo(), elapsed()
349*/
350auto QElapsedTimer::durationTo(const QElapsedTimer &other) const noexcept -> Duration
351{
352 Duration d1(t1);
353 Duration d2(other.t1);
354 return d2 - d1;
355}
356
357/*!
358 Returns the number of milliseconds between this QElapsedTimer and \a
359 other. If \a other was started before this object, the returned value
360 will be negative. If it was started later, the returned value will be
361 positive.
362
363 The return value is undefined if this object or \a other were invalidated.
364
365 \sa secsTo(), elapsed()
366*/
367qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const noexcept
368{
369 using namespace std::chrono;
370 return duration_cast<milliseconds>(durationTo(other)).count();
371}
372
373/*!
374 Returns the number of seconds between this QElapsedTimer and \a other. If
375 \a other was started before this object, the returned value will be
376 negative. If it was started later, the returned value will be positive.
377
378 Calling this function on or with a QElapsedTimer that is invalid
379 results in undefined behavior.
380
381 \sa msecsTo(), elapsed()
382*/
383qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const noexcept
384{
385 using namespace std::chrono;
386 return duration_cast<seconds>(durationTo(other)).count();
387}
388
389static const qint64 invalidData = Q_INT64_C(0x8000000000000000);
390
391/*!
392 \fn QElapsedTimer::invalidate() noexcept
393 Marks this QElapsedTimer object as invalid.
394
395 An invalid object can be checked with isValid(). Calculations of timer
396 elapsed since invalid data are undefined and will likely produce bizarre
397 results.
398
399 \sa isValid(), start(), restart()
400*/
401void QElapsedTimer::invalidate() noexcept
402{
403 t1 = t2 = invalidData;
404}
405
406/*!
407 Returns \c false if the timer has never been started or invalidated by a
408 call to invalidate().
409
410 \sa invalidate(), start(), restart()
411*/
412bool QElapsedTimer::isValid() const noexcept
413{
414 return t1 != invalidData && t2 != invalidData;
415}
416
417/*!
418 Returns \c true if elapsed() exceeds the given \a timeout, otherwise \c false.
419
420 A negative \a timeout is interpreted as infinite, so \c false is returned in
421 this case. Otherwise, this is equivalent to \c {elapsed() > timeout}. You
422 can do the same for a duration by comparing durationElapsed() to a duration
423 timeout.
424
425 \sa elapsed(), QDeadlineTimer
426*/
427bool QElapsedTimer::hasExpired(qint64 timeout) const noexcept
428{
429 // if timeout is -1, quint64(timeout) is LLINT_MAX, so this will be
430 // considered as never expired
431 return quint64(elapsed()) > quint64(timeout);
432}
433
434bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
435{
436 return lhs.t1 < rhs.t1;
437}
438
439QT_END_NAMESPACE
Combined button and popup list for selecting options.
bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
static const qint64 invalidData