QElapsedTimer

Inheritance diagram of QElapsedTimer

Note

This class was introduced in Qt 4.7

Synopsis

Functions

Static functions

Detailed Description

The PySide.QtCore.QElapsedTimer class provides a fast way to calculate elapsed times.

The PySide.QtCore.QElapsedTimer class is usually used to quickly calculate how much time has elapsed between two events. Its API is similar to that of PySide.QtCore.QTime , so code that was using that can be ported quickly to the new class.

However, unlike PySide.QtCore.QTime , PySide.QtCore.QElapsedTimer tries to use monotonic clocks if possible. This means it’s not possible to convert PySide.QtCore.QElapsedTimer 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()

sys.stderr.write("The slow operation took" + timer.elapsed() + "milliseconds")

In this example, the timer is started by a call to PySide.QtCore.QElapsedTimer.start() and the elapsed timer is calculated by the PySide.QtCore.QElapsedTimer.elapsed() 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 in PySide.QtCore.QIODevice 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, PySide.QtCore.QElapsedTimer provides the PySide.QtCore.QElapsedTimer.hasExpired() 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()

Reference clocks

PySide.QtCore.QElapsedTimer will use the platform’s monotonic reference clock in all platforms that support it (see QElapsedTimer.isMonotonic() ). This has the added benefit that PySide.QtCore.QElapsedTimer is immune to time adjustments, such as the user correcting the time. Also unlike PySide.QtCore.QTime , PySide.QtCore.QElapsedTimer is immune to changes in the timezone settings, such as daylight savings periods.

On the other hand, this means PySide.QtCore.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 the PySide.QtCore.QElapsedTimer object ( QElapsedTimer.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. PySide.QtCore.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 as PySide.QtCore.QElapsedTimer (see QElapsedTimer.clockType() ).

32-bit overflows

Some of the clocks that PySide.QtCore.QElapsedTimer have a limited range and may overflow after hitting the upper limit (usually 32-bit). PySide.QtCore.QElapsedTimer deals with this overflow issue and presents a consistent timing. However, when extracting the time since reference from PySide.QtCore.QElapsedTimer , two different processes in the same machine may have different understanding of how much time has actually elapsed.

The information on which clocks types may overflow and how to remedy that issue is documented along with the clock types.

class PySide.QtCore.QElapsedTimer
class PySide.QtCore.QElapsedTimer(QElapsedTimer)
Parameters:QElapsedTimerPySide.QtCore.QElapsedTimer
PySide.QtCore.QElapsedTimer.ClockType

This enum contains the different clock types that PySide.QtCore.QElapsedTimer may use.

PySide.QtCore.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 that PySide.QtCore.QElapsedTimer can be used with other non-Qt implementations, to guarantee that the same reference clock is being used.

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 PySide.QtCore.QElapsedTimer may use.

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 and does not overflow.
QElapsedTimer.TickCounter The system’s tick counter, used on Windows and Symbian systems. This clock may overflow.
QElapsedTimer.MachAbsoluteTime The Mach kernel’s absolute time (Mac OS X). This clock is monotonic and does not overflow.

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 ).

This clock does not overflow.

TickCounter

The tick counter clock type is based on the system’s or the processor’s tick counter, multiplied by the duration of a tick. This clock type is used on Windows and Symbian platforms.

The TickCounter clock type is the only clock type that may overflow. Windows Vista and Windows Server 2008 support the extended 64-bit tick counter, which allows avoiding the overflow.

On Windows systems, the clock overflows after 2^32 milliseconds, which corresponds to roughly 49.7 days. This means two processes’s reckoning of the time since the reference may be different by multiples of 2^32 milliseconds. When comparing such values, it’s recommended that the high 32 bits of the millisecond count be masked off.

On Symbian systems, the overflow happens after 2^32 ticks, the duration of which can be obtained from the platform HAL using the constant HAL::ENanoTickPeriod. When comparing values between processes, it’s necessary to divide the value by the tick duration and mask off the high 32 bits.

MachAbsoluteTime

This clock type is based on the absolute time presented by Mach kernels, such as that found on Mac OS X. This clock type is presented separately from MonotonicClock since Mac OS X is also a Unix system and may support a POSIX monotonic clock with values differing from the Mach absolute time.

This clock is monotonic and does not overflow.

Note

This enum was introduced or modified in Qt 4.7

static PySide.QtCore.QElapsedTimer.clockType()
Return type:PySide.QtCore.QElapsedTimer.ClockType
PySide.QtCore.QElapsedTimer.elapsed()
Return type:PySide.QtCore.qint64
PySide.QtCore.QElapsedTimer.hasExpired(timeout)
Parameters:timeoutPySide.QtCore.qint64
Return type:PySide.QtCore.bool
PySide.QtCore.QElapsedTimer.invalidate()
static PySide.QtCore.QElapsedTimer.isMonotonic()
Return type:PySide.QtCore.bool
PySide.QtCore.QElapsedTimer.isValid()
Return type:PySide.QtCore.bool
PySide.QtCore.QElapsedTimer.msecsSinceReference()
Return type:PySide.QtCore.qint64
PySide.QtCore.QElapsedTimer.msecsTo(other)
Parameters:otherPySide.QtCore.QElapsedTimer
Return type:PySide.QtCore.qint64
PySide.QtCore.QElapsedTimer.__ne__(other)
Parameters:otherPySide.QtCore.QElapsedTimer
Return type:PySide.QtCore.bool
PySide.QtCore.QElapsedTimer.__eq__(other)
Parameters:otherPySide.QtCore.QElapsedTimer
Return type:PySide.QtCore.bool
PySide.QtCore.QElapsedTimer.restart()
Return type:PySide.QtCore.qint64
PySide.QtCore.QElapsedTimer.secsTo(other)
Parameters:otherPySide.QtCore.QElapsedTimer
Return type:PySide.QtCore.qint64
PySide.QtCore.QElapsedTimer.start()