qelapsedtimer.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qelapsedtimer.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2015 The Qt Company Ltd.-
4** Contact: http://www.qt.io/licensing/-
5**-
6** This file is part of the QtCore module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL21$-
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 http://www.qt.io/terms-conditions. For further-
15** information use the contact form at http://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 2.1 or version 3 as published by the Free-
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and-
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the-
22** following information to ensure the GNU Lesser General Public License-
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and-
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.-
25**-
26** As a special exception, The Qt Company gives you certain additional-
27** rights. These rights are described in The Qt Company LGPL Exception-
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.-
29**-
30** $QT_END_LICENSE$-
31**-
32****************************************************************************/-
33-
34#include "qelapsedtimer.h"-
35-
36QT_BEGIN_NAMESPACE-
37-
38/*!-
39 \class QElapsedTimer-
40 \inmodule QtCore-
41 \brief The QElapsedTimer class provides a fast way to calculate elapsed times.-
42 \since 4.7-
43-
44 \reentrant-
45 \ingroup tools-
46-
47 The QElapsedTimer class is usually used to quickly calculate how much-
48 time has elapsed between two events. Its API is similar to that of QTime,-
49 so code that was using that can be ported quickly to the new class.-
50-
51 However, unlike QTime, QElapsedTimer tries to use monotonic clocks if-
52 possible. This means it's not possible to convert QElapsedTimer objects-
53 to a human-readable time.-
54-
55 The typical use-case for the class is to determine how much time was-
56 spent in a slow operation. The simplest example of such a case is for-
57 debugging purposes, as in the following example:-
58-
59 \snippet qelapsedtimer/main.cpp 0-
60-
61 In this example, the timer is started by a call to start() and the-
62 elapsed timer is calculated by the elapsed() function.-
63-
64 The time elapsed can also be used to recalculate the time available for-
65 another operation, after the first one is complete. This is useful when-
66 the execution must complete within a certain time period, but several-
67 steps are needed. The \tt{waitFor}-type functions in QIODevice and its-
68 subclasses are good examples of such need. In that case, the code could-
69 be as follows:-
70-
71 \snippet qelapsedtimer/main.cpp 1-
72-
73 Another use-case is to execute a certain operation for a specific-
74 timeslice. For this, QElapsedTimer provides the hasExpired() convenience-
75 function, which can be used to determine if a certain number of-
76 milliseconds has already elapsed:-
77-
78 \snippet qelapsedtimer/main.cpp 2-
79-
80 \section1 Reference Clocks-
81-
82 QElapsedTimer will use the platform's monotonic reference clock in all-
83 platforms that support it (see QElapsedTimer::isMonotonic()). This has-
84 the added benefit that QElapsedTimer is immune to time adjustments, such-
85 as the user correcting the time. Also unlike QTime, QElapsedTimer is-
86 immune to changes in the timezone settings, such as daylight-saving-
87 periods.-
88-
89 On the other hand, this means QElapsedTimer values can only be compared-
90 with other values that use the same reference. This is especially true if-
91 the time since the reference is extracted from the QElapsedTimer object-
92 (QElapsedTimer::msecsSinceReference()) and serialised. These values-
93 should never be exchanged across the network or saved to disk, since-
94 there's no telling whether the computer node receiving the data is the-
95 same as the one originating it or if it has rebooted since.-
96-
97 It is, however, possible to exchange the value with other processes-
98 running on the same machine, provided that they also use the same-
99 reference clock. QElapsedTimer will always use the same clock, so it's-
100 safe to compare with the value coming from another process in the same-
101 machine. If comparing to values produced by other APIs, you should check-
102 that the clock used is the same as QElapsedTimer (see-
103 QElapsedTimer::clockType()).-
104-
105 \section2 32-bit overflows-
106-
107 Some of the clocks used by QElapsedTimer have a limited range and may-
108 overflow after hitting the upper limit (usually 32-bit). QElapsedTimer-
109 deals with this overflow issue and presents a consistent timing. However,-
110 when extracting the time since reference from QElapsedTimer, two-
111 different processes in the same machine may have different understanding-
112 of how much time has actually elapsed.-
113-
114 The information on which clocks types may overflow and how to remedy that-
115 issue is documented along with the clock types.-
116-
117 \sa QTime, QTimer-
118*/-
119-
120/*!-
121 \enum QElapsedTimer::ClockType-
122-
123 This enum contains the different clock types that QElapsedTimer may use.-
124-
125 QElapsedTimer will always use the same clock type in a particular-
126 machine, so this value will not change during the lifetime of a program.-
127 It is provided so that QElapsedTimer can be used with other non-Qt-
128 implementations, to guarantee that the same reference clock is being-
129 used.-
130-
131 \value SystemTime The human-readable system time. This clock is not monotonic.-
132 \value MonotonicClock The system's monotonic clock, usually found in Unix systems. This clock is monotonic and does not overflow.-
133 \value TickCounter The system's tick counter, used on Windows systems. This clock may overflow.-
134 \value MachAbsoluteTime The Mach kernel's absolute time (\macos and iOS). This clock is monotonic and does not overflow.-
135 \value PerformanceCounter The high-resolution performance counter provided by Windows. This clock is monotonic and does not overflow.-
136-
137 \section2 SystemTime-
138-
139 The system time clock is purely the real time, expressed in milliseconds-
140 since Jan 1, 1970 at 0:00 UTC. It's equivalent to the value returned by-
141 the C and POSIX \tt{time} function, with the milliseconds added. This-
142 clock type is currently only used on Unix systems that do not support-
143 monotonic clocks (see below).-
144-
145 This is the only non-monotonic clock that QElapsedTimer may use.-
146-
147 \section2 MonotonicClock-
148-
149 This is the system's monotonic clock, expressed in milliseconds since an-
150 arbitrary point in the past. This clock type is used on Unix systems-
151 which support POSIX monotonic clocks (\tt{_POSIX_MONOTONIC_CLOCK}).-
152-
153 This clock does not overflow.-
154-
155 \section2 TickCounter-
156-
157 The tick counter clock type is based on the system's or the processor's-
158 tick counter, multiplied by the duration of a tick. This clock type is-
159 used on Windows platforms. If the high-precision performance-
160 counter is available on Windows, the \tt{PerformanceCounter} clock type-
161 is used instead.-
162-
163 The TickCounter clock type is the only clock type that may overflow.-
164 Windows Vista and Windows Server 2008 support the extended 64-bit tick-
165 counter, which allows avoiding the overflow.-
166-
167 On Windows systems, the clock overflows after 2^32 milliseconds, which-
168 corresponds to roughly 49.7 days. This means two processes' reckoning of-
169 the time since the reference may be different by multiples of 2^32-
170 milliseconds. When comparing such values, it's recommended that the high-
171 32 bits of the millisecond count be masked off.-
172-
173 \section2 MachAbsoluteTime-
174-
175 This clock type is based on the absolute time presented by Mach kernels,-
176 such as that found on \macos. This clock type is presented separately-
177 from MonotonicClock since \macos and iOS are also Unix systems and may support-
178 a POSIX monotonic clock with values differing from the Mach absolute-
179 time.-
180-
181 This clock is monotonic and does not overflow.-
182-
183 \section2 PerformanceCounter-
184-
185 This clock uses the Windows functions \tt{QueryPerformanceCounter} and-
186 \tt{QueryPerformanceFrequency} to access the system's high-precision-
187 performance counter. Since this counter may not be available on all-
188 systems, QElapsedTimer will fall back to the \tt{TickCounter} clock-
189 automatically, if this clock cannot be used.-
190-
191 This clock is monotonic and does not overflow.-
192-
193 \sa clockType(), isMonotonic()-
194*/-
195-
196/*!-
197 \fn QElapsedTimer::QElapsedTimer()-
198 \since 5.4-
199-
200 Constructs an invalid QElapsedTimer. A timer becomes valid once it has been-
201 started.-
202-
203 \sa isValid(), start()-
204*/-
205-
206-
207/*!-
208 \fn bool QElapsedTimer::operator ==(const QElapsedTimer &other) const-
209-
210 Returns \c true if this object and \a other contain the same time.-
211*/-
212-
213/*!-
214 \fn bool QElapsedTimer::operator !=(const QElapsedTimer &other) const-
215-
216 Returns \c true if this object and \a other contain different times.-
217*/-
218-
219static const qint64 invalidData = Q_INT64_C(0x8000000000000000);-
220-
221/*!-
222 Marks this QElapsedTimer object as invalid.-
223-
224 An invalid object can be checked with isValid(). Calculations of timer-
225 elapsed since invalid data are undefined and will likely produce bizarre-
226 results.-
227-
228 \sa isValid(), start(), restart()-
229*/-
230void QElapsedTimer::invalidate() Q_DECL_NOTHROW-
231{-
232 t1 = t2 = invalidData;-
233}
executed 134639 times by 81 tests: end of block
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_QAbstractAnimation
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAccessibility
  • tst_QCalendarWidget
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCompleter
  • tst_QDataWidgetMapper
  • tst_QDateTimeEdit
  • tst_QDebug
  • tst_QDirModel
  • tst_QDockWidget
  • tst_QElapsedTimer
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QFontComboBox
  • tst_QFontDialog
  • tst_QFuture
  • tst_QFutureSynchronizer
  • tst_QFutureWatcher
  • tst_QGestureRecognizer
  • ...
134639
234-
235/*!-
236 Returns \c false if the timer has never been started or invalidated by a-
237 call to invalidate().-
238-
239 \sa invalidate(), start(), restart()-
240*/-
241bool QElapsedTimer::isValid() const Q_DECL_NOTHROW-
242{-
243 return t1 != invalidData && t2 != invalidData;
executed 153606 times by 47 tests: return t1 != invalidData && t2 != invalidData;
Executed by:
  • tst_Gestures
  • tst_QAbstractAnimation
  • tst_QAbstractItemView
  • tst_QAccessibility
  • tst_QColumnView
  • tst_QComboBox
  • tst_QDebug
  • tst_QDockWidget
  • tst_QElapsedTimer
  • tst_QFuture
  • tst_QFutureWatcher
  • tst_QGraphicsProxyWidget
  • tst_QLineEdit
  • tst_QListView
  • tst_QListWidget
  • tst_QLockFile
  • tst_QMainWindow
  • tst_QMdiArea
  • tst_QMdiSubWindow
  • tst_QMenu
  • tst_QMimeDatabase
  • tst_QNetworkReply
  • tst_QOpenGLWidget
  • tst_QParallelAnimationGroup
  • tst_QPauseAnimation
  • ...
t1 != invalidDataDescription
TRUEevaluated 119816 times by 28 tests
Evaluated by:
  • tst_QAbstractAnimation
  • tst_QAbstractItemView
  • tst_QColumnView
  • tst_QComboBox
  • tst_QDebug
  • tst_QElapsedTimer
  • tst_QFuture
  • tst_QFutureWatcher
  • tst_QLineEdit
  • tst_QListView
  • tst_QMainWindow
  • tst_QMimeDatabase
  • tst_QNetworkReply
  • tst_QOpenGLWidget
  • tst_QParallelAnimationGroup
  • tst_QPauseAnimation
  • tst_QProgressBar
  • tst_QPropertyAnimation
  • tst_QScroller
  • tst_QSequentialAnimationGroup
  • tst_QStateMachine
  • tst_QTableView
  • tst_QTreeView
  • tst_QtConcurrentFilter
  • tst_QtConcurrentIterateKernel
  • ...
FALSEevaluated 33790 times by 46 tests
Evaluated by:
  • tst_Gestures
  • tst_QAbstractAnimation
  • tst_QAbstractItemView
  • tst_QAccessibility
  • tst_QColumnView
  • tst_QComboBox
  • tst_QDebug
  • tst_QDockWidget
  • tst_QElapsedTimer
  • tst_QFuture
  • tst_QFutureWatcher
  • tst_QGraphicsProxyWidget
  • tst_QLineEdit
  • tst_QListView
  • tst_QListWidget
  • tst_QLockFile
  • tst_QMainWindow
  • tst_QMdiArea
  • tst_QMdiSubWindow
  • tst_QMenu
  • tst_QMimeDatabase
  • tst_QNetworkReply
  • tst_QParallelAnimationGroup
  • tst_QPauseAnimation
  • tst_QPrinter
  • ...
t2 != invalidDataDescription
TRUEevaluated 119816 times by 28 tests
Evaluated by:
  • tst_QAbstractAnimation
  • tst_QAbstractItemView
  • tst_QColumnView
  • tst_QComboBox
  • tst_QDebug
  • tst_QElapsedTimer
  • tst_QFuture
  • tst_QFutureWatcher
  • tst_QLineEdit
  • tst_QListView
  • tst_QMainWindow
  • tst_QMimeDatabase
  • tst_QNetworkReply
  • tst_QOpenGLWidget
  • tst_QParallelAnimationGroup
  • tst_QPauseAnimation
  • tst_QProgressBar
  • tst_QPropertyAnimation
  • tst_QScroller
  • tst_QSequentialAnimationGroup
  • tst_QStateMachine
  • tst_QTableView
  • tst_QTreeView
  • tst_QtConcurrentFilter
  • tst_QtConcurrentIterateKernel
  • ...
FALSEnever evaluated
0-153606
244}-
245-
246/*!-
247 Returns \c true if this QElapsedTimer has already expired by \a timeout-
248 milliseconds (that is, more than \a timeout milliseconds have elapsed).-
249 The value of \a timeout can be -1 to indicate that this timer does not-
250 expire, in which case this function will always return false.-
251-
252 \sa elapsed()-
253*/-
254bool QElapsedTimer::hasExpired(qint64 timeout) const Q_DECL_NOTHROW-
255{-
256 // if timeout is -1, quint64(timeout) is LLINT_MAX, so this will be-
257 // considered as never expired-
258 return quint64(elapsed()) > quint64(timeout);
executed 77714 times by 3 tests: return quint64(elapsed()) > quint64(timeout);
Executed by:
  • tst_QElapsedTimer
  • tst_QEventDispatcher
  • tst_QLockFile
77714
259}-
260-
261QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9