QElapsedTimer Class
The QElapsedTimer class provides a fast way to calculate elapsed times. More…
| Header | #include <QElapsedTimer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Types
Public Functions
| void | start() |
| qint64 | restart() |
| void | invalidate() |
| bool | isValid() const |
| qint64 | nsecsElapsed() const |
| std::chrono::nanoseconds | durationElapsed() const |
| qint64 | elapsed() const |
| bool | hasExpired(qint64 timeoutMs) const |
| qint64 | msecsSinceReference() const |
| qint64 | msecsTo(const QElapsedTimer &o) const |
| qint64 | secsTo(const QElapsedTimer &o) const |
| std::chrono::nanoseconds | durationTo(const QElapsedTimer &o) const |
Static Public Members
| ClockType | clockType() |
| bool | isMonotonic() |
Detailed Description
Call start() or restart() to begin timing, then elapsed() or nsecsElapsed() to read the time since then. The timer uses a monotonic clock, so it is not affected by changes to the system time.
QElapsedTimer timer;
timer.start();
slowOperation();
qDebug() << "took" << timer.elapsed() << "ms";
See also QDeadlineTimer.
Differences from Qt
The timer is always based on std::chrono::steady_clock, so clockType() always returns MonotonicClock and isMonotonic() always returns true. hasExpired() treats an invalid timer as expired.
Member Type Documentation
typedef QElapsedTimer::Clock
The clock that the timer reads, std::chrono::steady_clock.
enum QElapsedTimer::ClockType
Describes the clock that a timer uses.
| Constant | Description |
|---|---|
QElapsedTimer::SystemTime | The system time. Not used by Nt. |
QElapsedTimer::MonotonicClock | A monotonic clock. This is the clock that Nt uses. |
QElapsedTimer::TickCounter | A tick counter. Not used by Nt. |
QElapsedTimer::MachAbsoluteTime | The Mach absolute time. Not used by Nt. |
QElapsedTimer::PerformanceCounter | A high-resolution performance counter. Not used by Nt. |
See also clockType().
Member Function Documentation
ClockType QElapsedTimer::clockType()
Returns the clock type that the timer uses. This is always MonotonicClock.
See also isMonotonic().
std::chrono::nanoseconds QElapsedTimer::durationElapsed() const
Returns a std::chrono::nanoseconds duration with the time since the timer was started, or zero if the timer is invalid.
This function was introduced in 6.6.
See also nsecsElapsed().
std::chrono::nanoseconds QElapsedTimer::durationTo(const QElapsedTimer &o) const
Returns the time from this timer's start time to the start time of o as a std::chrono::nanoseconds duration.
This function was introduced in 6.6.
See also msecsTo().
qint64 QElapsedTimer::elapsed() const
Returns the number of milliseconds since the timer was started, or 0 if the timer is invalid.
See also nsecsElapsed() and hasExpired().
bool QElapsedTimer::hasExpired(qint64 timeoutMs) const
Returns true if more than timeoutMs milliseconds have elapsed since the timer was started, or if the timer is invalid; otherwise returns false.
See also elapsed().
void QElapsedTimer::invalidate()
Marks the timer as invalid.
See also isValid().
bool QElapsedTimer::isMonotonic()
Returns true if the clock is monotonic, which is always the case.
See also clockType().
bool QElapsedTimer::isValid() const
Returns true if start() or restart() has been called and the timer has not been invalidated; otherwise returns false.
See also invalidate().
qint64 QElapsedTimer::msecsSinceReference() const
Returns the start time of the timer as milliseconds since the clock's reference point.
See also msecsTo().
qint64 QElapsedTimer::msecsTo(const QElapsedTimer &o) const
Returns the number of milliseconds from this timer's start time to the start time of o. The result is negative if o was started earlier.
See also msecsSinceReference().
qint64 QElapsedTimer::nsecsElapsed() const
Returns the number of nanoseconds since the timer was started, or 0 if the timer is invalid.
See also elapsed().
qint64 QElapsedTimer::restart()
Restarts the timer and returns the number of milliseconds that had elapsed since it was started.
See also start() and elapsed().
qint64 QElapsedTimer::secsTo(const QElapsedTimer &o) const
Returns the number of seconds from this timer's start time to the start time of o. The result is negative if o was started earlier.
See also msecsTo().
void QElapsedTimer::start()
Starts the timer and makes it valid.