QElapsedTimer 類

QElapsedTimer 類提供計算消耗時間的快速方式。 更多...

頭: #include <QElapsedTimer>
qmake: QT += core
Since: Qt 4.7

該類在 Qt 4.7 引入。

注意: 此類的所有函數 可重入 .

公共類型

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

公共函數

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

靜態公共成員

QElapsedTimer::ClockType clockType ()
bool isMonotonic ()
bool operator< (const QElapsedTimer & v1 , const QElapsedTimer & v2 )

詳細描述

QElapsedTimer 類通常用於快速計算 2 事件之間消耗瞭多少時間。其 API 類似於 QTime ,因此,正使用代碼可以快速移植到新類。

不管怎樣,不像 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:

    QElapsedTimer timer;
    timer.start();
    slowOperation1();
    qDebug() << "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 the elapsed () 函數。

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 QIODevice and its subclasses are good examples of such need. In that case, the code could be as follows:

void executeSlowOperations(int timeout)
{
    QElapsedTimer timer;
    timer.start();
    slowOperation1();
    int 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 the hasExpired () convenience function, which can be used to determine if a certain number of milliseconds has already elapsed:

void executeOperationsForTime(int ms)
{
    QElapsedTimer timer;
    timer.start();
    while (!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.

參考時鍾

QElapsedTimer will use the platform's monotonic reference clock in all platforms that support it (see QElapsedTimer::isMonotonic ()). This has the added benefit that QElapsedTimer is immune to time adjustments, such as the user correcting the time. Also unlike QTime , 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 the 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. 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 QElapsedTimer (see QElapsedTimer::clockType ()).

另請參閱 QTime , QTimer ,和 QDeadlineTimer .

成員類型文檔編製

enum QElapsedTimer:: 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.

常量 描述
QElapsedTimer::SystemTime 0 The human-readable system time. This clock is not monotonic.
QElapsedTimer::MonotonicClock 1 The system's monotonic clock, usually found in Unix systems. This clock is monotonic.
QElapsedTimer::TickCounter 2 Not used anymore.
QElapsedTimer::MachAbsoluteTime 3 The Mach kernel's absolute time (macOS and iOS). This clock is monotonic.
QElapsedTimer::PerformanceCounter 4 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 and QueryPerformanceFrequency to access the system's performance counter.

This clock is monotonic.

另請參閱 clockType () 和 isMonotonic ().

成員函數文檔編製

QElapsedTimer:: QElapsedTimer ()

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

該函數在 Qt 5.4 引入。

另請參閱 isValid () 和 start ().

[static] QElapsedTimer::ClockType QElapsedTimer:: clockType ()

Returns the clock type that this QElapsedTimer implementation uses.

另請參閱 isMonotonic ().

qint64 QElapsedTimer:: elapsed () const

Returns the number of milliseconds since this QElapsedTimer was last started.

Calling this function on a QElapsedTimer that is invalid results in undefined behavior.

另請參閱 start (), restart (), hasExpired (), isValid (),和 invalidate ().

bool QElapsedTimer:: hasExpired ( qint64 timeout ) const

返迴 true 若此 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.

另請參閱 elapsed () 和 QDeadlineTimer .

void QElapsedTimer:: 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.

另請參閱 isValid (), start (),和 restart ().

[static] bool QElapsedTimer:: isMonotonic ()

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

另請參閱 clockType () 和 QElapsedTimer::ClockType .

bool QElapsedTimer:: isValid () const

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

另請參閱 invalidate (), start (),和 restart ().

qint64 QElapsedTimer:: msecsSinceReference () const

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.

另請參閱 clockType () 和 elapsed ().

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

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

另請參閱 secsTo () 和 elapsed ().

qint64 QElapsedTimer:: nsecsElapsed () const

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.

該函數在 Qt 4.8 引入。

另請參閱 start (), restart (), hasExpired (),和 invalidate ().

qint64 QElapsedTimer:: restart ()

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:

    QElapsedTimer timer;
    int count = 1;
    timer.start();
    do {
        count *= 2;
        slowOperation2(count);
    } while (timer.restart() < 250);
    return count;
					

另請參閱 start (), invalidate (), elapsed (),和 isValid ().

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

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

另請參閱 msecsTo () 和 elapsed ().

void QElapsedTimer:: start ()

啓動此計時器。一旦被啓動,可以校驗計時器值采用 elapsed () 或 msecsSinceReference ().

通常,僅僅啓動計時器在長時間操作前,譬如:

    QElapsedTimer timer;
    timer.start();
    slowOperation1();
    qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds";
					

此外,啓動計時器使之再次有效。

另請參閱 restart (), invalidate (),和 elapsed ().

bool QElapsedTimer:: operator!= (const QElapsedTimer & other ) const

返迴 true if this object and other contain different times.

bool QElapsedTimer:: operator== (const QElapsedTimer & other ) const

返迴 true if this object and other contain the same time.

相關非成員

bool operator< (const QElapsedTimer & v1 , const QElapsedTimer & v2 )

返迴 true if v1 was started before v2 , 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.