- class QElapsedTimer¶
The
QElapsedTimer
class provides a fast way to calculate elapsed times. More…Added in version 4.7.
Synopsis¶
Methods¶
def
__init__()
def
elapsed()
def
hasExpired()
def
invalidate()
def
isValid()
def
msecsTo()
def
nsecsElapsed()
def
__ne__()
def
__le__()
def
__eq__()
def
__gt__()
def
__ge__()
def
restart()
def
secsTo()
def
start()
Static functions¶
def
clockType()
def
isMonotonic()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
The
QElapsedTimer
class is usually used to quickly calculate how much time has elapsed between two events. Its API is similar to that ofQTime
, so code that was using that can be ported quickly to the new class.However, unlike
QTime
,QElapsedTimer
tries to use monotonic clocks if possible. This means it’s not possible to convertQElapsedTimer
objects to a human-readable time.The typical use-case for the class is to determine how much time was spent in a slow operation. The simplest example of such a case is for debugging purposes, as in the following example:
timer = QElapsedTimer() timer.start() slowOperation1() print("The slow operation took", timer.elapsed(), "milliseconds")
In this example, the timer is started by a call to
start()
and the elapsed time is calculated by theelapsed()
function.The time elapsed can also be used to recalculate the time available for another operation, after the first one is complete. This is useful when the execution must complete within a certain time period, but several steps are needed. The
waitFor
-type functions inQIODevice
and its subclasses are good examples of such need. In that case, the code could be as follows:def executeSlowOperations(timeout): timer = QElapsedTimer() timer.start() slowOperation1() remainingTime = timeout - timer.elapsed() if remainingTime > 0: slowOperation2(remainingTime)
Another use-case is to execute a certain operation for a specific timeslice. For this,
QElapsedTimer
provides thehasExpired()
convenience function, which can be used to determine if a certain number of milliseconds has already elapsed:def executeOperationsForTime(ms): timer = QElapsedTimer() timer.start() while not timer.hasExpired(ms): slowOperation1()
It is often more convenient to use
QDeadlineTimer
in this case, which counts towards a timeout in the future instead of tracking elapsed time.Reference Clocks¶
QElapsedTimer
will use the platform’s monotonic reference clock in all platforms that support it (seeisMonotonic()
). This has the added benefit thatQElapsedTimer
is immune to time adjustments, such as the user correcting the time. Also unlikeQTime
,QElapsedTimer
is immune to changes in the timezone settings, such as daylight-saving periods.On the other hand, this means
QElapsedTimer
values can only be compared with other values that use the same reference. This is especially true if the time since the reference is extracted from theQElapsedTimer
object (msecsSinceReference()
) and serialised. These values should never be exchanged across the network or saved to disk, since there’s no telling whether the computer node receiving the data is the same as the one originating it or if it has rebooted since.It is, however, possible to exchange the value with other processes running on the same machine, provided that they also use the same reference clock.
QElapsedTimer
will always use the same clock, so it’s safe to compare with the value coming from another process in the same machine. If comparing to values produced by other APIs, you should check that the clock used is the same asQElapsedTimer
(seeclockType()
).See also
QTime
QChronoTimer
QDeadlineTimer
- class ClockType¶
This enum contains the different clock types that
QElapsedTimer
may use.QElapsedTimer
will always use the same clock type in a particular machine, so this value will not change during the lifetime of a program. It is provided so thatQElapsedTimer
can be used with other non-Qt implementations, to guarantee that the same reference clock is being used.Constant
Description
QElapsedTimer.SystemTime
The human-readable system time. This clock is not monotonic.
QElapsedTimer.MonotonicClock
The system’s monotonic clock, usually found in Unix systems. This clock is monotonic.
QElapsedTimer.TickCounter
Not used anymore.
QElapsedTimer.MachAbsoluteTime
The Mach kernel’s absolute time (macOS and iOS). This clock is monotonic.
QElapsedTimer.PerformanceCounter
The performance counter provided by Windows. This clock is monotonic.
SystemTime¶
The system time clock is purely the real time, expressed in milliseconds since Jan 1, 1970 at 0:00 UTC. It’s equivalent to the value returned by the C and POSIX
time
function, with the milliseconds added. This clock type is currently only used on Unix systems that do not support monotonic clocks (see below).This is the only non-monotonic clock that
QElapsedTimer
may use.MonotonicClock¶
This is the system’s monotonic clock, expressed in milliseconds since an arbitrary point in the past. This clock type is used on Unix systems which support POSIX monotonic clocks (
_POSIX_MONOTONIC_CLOCK
).MachAbsoluteTime¶
This clock type is based on the absolute time presented by Mach kernels, such as that found on macOS. This clock type is presented separately from MonotonicClock since macOS and iOS are also Unix systems and may support a POSIX monotonic clock with values differing from the Mach absolute time.
This clock is monotonic.
PerformanceCounter¶
This clock uses the Windows functions
QueryPerformanceCounter
andQueryPerformanceFrequency
to access the system’s performance counter.This clock is monotonic.
See also
Added in version 4.7.
- __init__()¶
Constructs an invalid
QElapsedTimer
. A timer becomes valid once it has been started.Returns the clock type that this
QElapsedTimer
implementation uses.Since Qt 6.6,
QElapsedTimer
usesstd::chrono::steady_clock
, so the clock type is alwaysMonotonicClock
.See also
- elapsed()¶
- Return type:
int
Returns the number of milliseconds since this
QElapsedTimer
was last started.Calling this function on a
QElapsedTimer
that is invalid results in undefined behavior.See also
- hasExpired(timeout)¶
- Parameters:
timeout – int
- Return type:
bool
Returns
true
ifelapsed()
exceeds the giventimeout
, otherwisefalse
.A negative
timeout
is interpreted as infinite, sofalse
is returned in this case. Otherwise, this is equivalent toelapsed() > timeout
. You can do the same for a duration by comparingdurationElapsed()
to a duration timeout.See also
- invalidate()¶
Marks this
QElapsedTimer
object as invalid.An invalid object can be checked with
isValid()
. Calculations of timer elapsed since invalid data are undefined and will likely produce bizarre results.- static isMonotonic()¶
- Return type:
bool
Returns
true
if this is a monotonic clock, false otherwise. See the information on the different clock types to understand which ones are monotonic.Since Qt 6.6,
QElapsedTimer
usesstd::chrono::steady_clock
, so this function now always returns true.See also
- isValid()¶
- Return type:
bool
Returns
false
if the timer has never been started or invalidated by a call toinvalidate()
.See also
- msecsSinceReference()¶
- Return type:
int
Returns the number of milliseconds between last time this
QElapsedTimer
object was started and its reference clock’s start.This number is usually arbitrary for all clocks except the
SystemTime
clock. For that clock type, this number is the number of milliseconds since January 1st, 1970 at 0:00 UTC (that is, it is the Unix time expressed in milliseconds).On Linux, Windows and Apple platforms, this value is usually the time since the system boot, though it usually does not include the time the system has spent in sleep states.
See also
- msecsTo(other)¶
- Parameters:
other –
QElapsedTimer
- Return type:
int
Returns the number of milliseconds between this
QElapsedTimer
andother
. Ifother
was started before this object, the returned value will be negative. If it was started later, the returned value will be positive.The return value is undefined if this object or
other
were invalidated.- nsecsElapsed()¶
- Return type:
int
Returns the number of nanoseconds since this
QElapsedTimer
was last started.Calling this function on a
QElapsedTimer
that is invalid results in undefined behavior.On platforms that do not provide nanosecond resolution, the value returned will be the best estimate available.
See also
- __ne__(rhs)¶
- Parameters:
rhs –
QElapsedTimer
- Return type:
bool
Returns
true
iflhs
andrhs
contain different times, false otherwise.- __le__(rhs)¶
- Parameters:
rhs –
QElapsedTimer
- Return type:
bool
- __eq__(rhs)¶
- Parameters:
rhs –
QElapsedTimer
- Return type:
bool
Returns
true
iflhs
andrhs
contain the same time, false otherwise.- __gt__(rhs)¶
- Parameters:
rhs –
QElapsedTimer
- Return type:
bool
- __ge__(rhs)¶
- Parameters:
rhs –
QElapsedTimer
- Return type:
bool
- restart()¶
- Return type:
int
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Restarts the timer and returns the number of milliseconds elapsed since the previous start. This function is equivalent to obtaining the elapsed time with
elapsed()
and then starting the timer again withstart()
, but it does so in one single operation, avoiding the need to obtain the clock value twice.Calling this function on a
QElapsedTimer
that is invalid results in undefined behavior.The following example illustrates how to use this function to calibrate a parameter to a slow operation (for example, an iteration count) so that this operation takes at least 250 milliseconds:
timer = QElapsedTimer() count = 1 timer.start() do { = 2 slowOperation2(count) } while (timer.restart() < 250) return count
See also
- secsTo(other)¶
- Parameters:
other –
QElapsedTimer
- Return type:
int
Returns the number of seconds between this
QElapsedTimer
andother
. Ifother
was started before this object, the returned value will be negative. If it was started later, the returned value will be positive.Calling this function on or with a
QElapsedTimer
that is invalid results in undefined behavior.- start()¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Starts this timer. Once started, a timer value can be checked with
elapsed()
ormsecsSinceReference()
.Normally, a timer is started just before a lengthy operation, such as:
timer = QElapsedTimer() timer.start() slowOperation1() print("The slow operation took", timer.elapsed(), "milliseconds")
Also, starting a timer makes it valid again.
See also