QtBase  v6.3.1
Public Types | Public Member Functions | Static Public Member Functions | Friends | Related Functions | List of all members
QElapsedTimer Class Reference

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

#include <qelapsedtimer.h>

Public Types

enum  ClockType {
  SystemTime , MonotonicClock , TickCounter , MachAbsoluteTime ,
  PerformanceCounter
}
 

Public Member Functions

constexpr QElapsedTimer ()
 
void start () noexcept
 
qint64 restart () noexcept
 
void invalidate () noexcept
 
bool isValid () const noexcept
 
qint64 nsecsElapsed () const noexcept
 
qint64 elapsed () const noexcept
 
bool hasExpired (qint64 timeout) const noexcept
 
qint64 msecsSinceReference () const noexcept
 
qint64 msecsTo (const QElapsedTimer &other) const noexcept
 
qint64 secsTo (const QElapsedTimer &other) const noexcept
 

Static Public Member Functions

static ClockType clockType () noexcept
 
static bool isMonotonic () noexcept
 

Friends

bool operator== (const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
 
bool operator!= (const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
 
bool Q_CORE_EXPORT operator< (const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
 

Related Functions

(Note that these are not member functions.)

bool operator< (const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
 

Detailed Description

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

\inmodule QtCore

Since
4.7

\reentrant

The QElapsedTimer class is usually used to quickly calculate how much time has elapsed between two events. Its API is similar to that of QTime, 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 convert 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:

In this example, the timer is started by a call to start() and the elapsed time is calculated by the 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 \tt{waitFor}-type functions in QIODevice and its subclasses are good examples of such need. In that case, the code could be as follows:

Another use-case is to execute a certain operation for a specific timeslice. For this, QElapsedTimer provides the hasExpired() convenience function, which can be used to determine if a certain number of milliseconds has already elapsed:

It is often more convenient to use \l{QDeadlineTimer} in this case, which counts towards a timeout in the future instead of tracking elapsed time.

Definition at line 48 of file qelapsedtimer.h.

Member Enumeration Documentation

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

\value SystemTime The human-readable system time. This clock is not monotonic. \value MonotonicClock The system's monotonic clock, usually found in Unix systems. This clock is monotonic. \value TickCounter Not used anymore. \value MachAbsoluteTime The Mach kernel's absolute time (\macos and iOS). This clock is monotonic. \value PerformanceCounter The performance counter provided by Windows. This clock is monotonic.

Enumerator
SystemTime 
MonotonicClock 
TickCounter 
MachAbsoluteTime 
PerformanceCounter 

Definition at line 51 of file qelapsedtimer.h.

Constructor & Destructor Documentation

◆ QElapsedTimer()

QElapsedTimer::QElapsedTimer ( )
inlineconstexpr
Since
5.4

Constructs an invalid QElapsedTimer. A timer becomes valid once it has been started.

See also
isValid(), start()

Definition at line 59 of file qelapsedtimer.h.

Member Function Documentation

◆ clockType()

QElapsedTimer::ClockType QElapsedTimer::clockType ( )
staticnoexcept

Returns the clock type that this QElapsedTimer implementation uses.

See also
isMonotonic()

Definition at line 51 of file qelapsedtimer_generic.cpp.

Here is the caller graph for this function:

◆ elapsed()

qint64 QElapsedTimer::elapsed ( ) const
noexcept

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
start(), restart(), hasExpired(), isValid(), invalidate()

Definition at line 136 of file qelapsedtimer_generic.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasExpired()

bool QElapsedTimer::hasExpired ( qint64  timeout) const
noexcept

Returns true if this QElapsedTimer has already expired by timeout milliseconds (that is, more than timeout milliseconds have elapsed). The value of timeout can be -1 to indicate that this timer does not expire, in which case this function will always return false.

See also
elapsed(), QDeadlineTimer

Definition at line 239 of file qelapsedtimer.cpp.

◆ invalidate()

void QElapsedTimer::invalidate ( )
noexcept

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.

See also
isValid(), start(), restart()

Definition at line 215 of file qelapsedtimer.cpp.

Here is the caller graph for this function:

◆ isMonotonic()

bool QElapsedTimer::isMonotonic ( )
staticnoexcept

Returns true if this is a monotonic clock, false otherwise. See the information on the different clock types to understand which ones are monotonic.

See also
clockType(), QElapsedTimer::ClockType

Definition at line 63 of file qelapsedtimer_generic.cpp.

Here is the caller graph for this function:

◆ isValid()

bool QElapsedTimer::isValid ( ) const
noexcept

Returns false if the timer has never been started or invalidated by a call to invalidate().

See also
invalidate(), start(), restart()

Definition at line 226 of file qelapsedtimer.cpp.

Here is the caller graph for this function:

◆ msecsSinceReference()

qint64 QElapsedTimer::msecsSinceReference ( ) const
noexcept

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 QElapsedTimer::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
clockType(), elapsed()

Definition at line 156 of file qelapsedtimer_generic.cpp.

◆ msecsTo()

qint64 QElapsedTimer::msecsTo ( const QElapsedTimer other) const
noexcept

Returns the number of milliseconds between this QElapsedTimer and other. If other 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.

See also
secsTo(), elapsed()

Definition at line 171 of file qelapsedtimer_generic.cpp.

Here is the call graph for this function:

◆ nsecsElapsed()

qint64 QElapsedTimer::nsecsElapsed ( ) const
noexcept
Since
4.8

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
start(), restart(), hasExpired(), invalidate()

Definition at line 122 of file qelapsedtimer_generic.cpp.

Here is the call graph for this function:

◆ restart()

qint64 QElapsedTimer::restart ( )
noexcept

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 with start(), 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:

See also
start(), invalidate(), elapsed(), isValid()

Definition at line 101 of file qelapsedtimer_generic.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ secsTo()

qint64 QElapsedTimer::secsTo ( const QElapsedTimer other) const
noexcept

Returns the number of seconds between this QElapsedTimer and other. If other 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.

See also
msecsTo(), elapsed()

Definition at line 187 of file qelapsedtimer_generic.cpp.

Here is the call graph for this function:

◆ start()

void QElapsedTimer::start ( )
noexcept

Starts this timer. Once started, a timer value can be checked with elapsed() or msecsSinceReference().

Normally, a timer is started just before a lengthy operation, such as:

Also, starting a timer makes it valid again.

See also
restart(), invalidate(), elapsed()

Definition at line 78 of file qelapsedtimer_generic.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator!=

bool QElapsedTimer::operator!= ( const QElapsedTimer lhs,
const QElapsedTimer rhs 
)
friend

Returns true if lhs and rhs contain different times, false otherwise.

Definition at line 83 of file qelapsedtimer.h.

◆ operator<() [1/2]

bool operator< ( const QElapsedTimer lhs,
const QElapsedTimer rhs 
)
related

Returns true if lhs was started before rhs, false otherwise.

The returned value is undefined if one of the two parameters is invalid and the other isn't. However, two invalid timers are equal and thus this function will return false.

Definition at line 192 of file qelapsedtimer_generic.cpp.

◆ operator< [2/2]

bool operator< ( const QElapsedTimer lhs,
const QElapsedTimer rhs 
)
friend

Returns true if lhs was started before rhs, false otherwise.

The returned value is undefined if one of the two parameters is invalid and the other isn't. However, two invalid timers are equal and thus this function will return false.

Definition at line 192 of file qelapsedtimer_generic.cpp.

◆ operator==

bool QElapsedTimer::operator== ( const QElapsedTimer lhs,
const QElapsedTimer rhs 
)
friend

Returns true if lhs and rhs contain the same time, false otherwise.

Definition at line 81 of file qelapsedtimer.h.


The documentation for this class was generated from the following files: