Elapsed Time Counter

Posted on  by admin

Modified3 months ago. With the Time module in python is it possible to measure elapsed time?

Header:#include
qmake: QT += core
Since: Qt 4.7

If so, how do I do that? I need to do this so that if the cursor has been in a widget for a certain duration an event happens.

9090 gold badges194194 silver badges270270 bronze badges.

Not the answer you're looking for? Browse other questions tagged pythontimeelapsed or ask your own question.

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

Update for Python 3.7

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

Update for Python 3.8

QElapsedTimer::ClockType clockType()
bool isMonotonic()

Related Non-Members

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

Detailed Description

270270 bronze badges. You can also write simple decorator to simplify measurement of execution time of various functions:. You can profile more then one function simultaneously.

Then to print measurements just call the print_prof_data():. 11 gold badge1414 silver badges1010 bronze badges. 1010 bronze badges. For users that want better formatting,.

will print out, for 2 seconds:. and for 7 minutes one second:. note that the minimum time unit with gmtime is seconds. If you need microseconds consider the following:.

strftime documentation. 22 gold badges2929 silver badges4040 bronze badges. 4040 bronze badges. For the best measure of elapsed time (since Python 3.3), use time.perf_counter().

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

For measurements on the order of hours/days, you don't care about sub-second resolution so use time.monotonic() instead. Return the value (in fractional seconds) of a monotonic clock, i.e.

a clock that cannot go backwards. The clock is not affected by system clock updates. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

Reference Clocks

In many implementations, these may actually be the same thing. Before 3.3, you're stuck with time.clock(). On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond. New in Python 3.7 is PEP 564 -- Add new time functions with nanosecond resolution. Use of these can further eliminate rounding and floating-point errors, especially if you're measuring very short periods, or your application (or Windows machine) is long-running.

Resolution starts breaking down on perf_counter() after around 100 days. So for example after a year of uptime, the shortest interval (greater than 0) it can measure will be bigger than when it started. time.clock is now gone. 1212 gold badges114114 silver badges192192 bronze badges. 192192 bronze badges. Another nice way to time things is to use the with python structure.

32-bit overflows

with structure is automatically calling __enter__ and __exit__ methods which is exactly what we need to time things. Let's create a Timer class. Then, one can use the Timer class like this:. The result is the following:. Primes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337,347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433,439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499].

Elapsed time to compute some prime numbers: 5.01704216003418ms. In programming, there are 2 main ways to measure time, with different results:.

Processor Time: This is how long this specific process spends actively being executed on the CPU.

Member Type Documentation

enum QElapsedTimer::ClockType

Sleep, waiting for a web request, or time when only other processes are executed will not contribute to this.

Use time.process_time(). Wall-Clock Time: This refers to how much time has passed "on a clock hanging on the wall", i.e. outside real time. Use time.perf_counter(). time.time() also measures wall-clock time but can be reset, so you could go back in time.

ConstantValueDescription
QElapsedTimer::SystemTime0The human-readable system time. This clock is not monotonic.
QElapsedTimer::MonotonicClock1The system's monotonic clock, usually found in Unix systems. This clock is monotonic and does not overflow.
QElapsedTimer::TickCounter2Not used anymore.
QElapsedTimer::MachAbsoluteTime3The Mach kernel's absolute time (macOS and iOS). This clock is monotonic and does not overflow.
QElapsedTimer::PerformanceCounter4The performance counter provided by Windows. This clock is monotonic and does not overflow.
SystemTime

time.monotonic() cannot be reset (monotonic = only goes forward) but has lower precision than time.perf_counter(). 55 gold badges4949 silver badges6262 bronze badges. 6262 bronze badges. For a longer period. if more than 24 hours. That is inspired by Rutger Hofste's answer.

Thank you Rutger! 11 gold badge88 silver badges1515 bronze badges.

MonotonicClock

1515 bronze badges. Vadim Shender response is great. You can also use a simpler decorator like below:. You need to import time and then use time.time() method to know current time. 11 gold badge3030 silver badges3535 bronze badges.

3535 bronze badges. time.time() will do the job.

MachAbsoluteTime

You may want to look at this question, but I don't think it will be necessary. 66 gold badges3939 silver badges5555 bronze badges.

5555 bronze badges.

PerformanceCounter

Here is an update to Vadim Shender's clever code with tabular output:.

This clock is monotonic and does not overflow.

See also clockType() and isMonotonic().

Member Function Documentation

QElapsedTimer::QElapsedTimer()

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

This function was introduced in Qt 5.4.

See also isValid() and start().

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

Returns the clock type that this QElapsedTimer implementation uses.

See also 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.

See also start(), restart(), hasExpired(), isValid(), and invalidate().

bool QElapsedTimer::hasExpired(qint64timeout) const

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

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

[static] bool QElapsedTimer::isMonotonic()

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() and QElapsedTimer::ClockType.

bool QElapsedTimer::isValid() const

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

See also invalidate(), start(), and 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.

See also clockType() and elapsed().

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

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

This function was introduced in Qt 4.8.

See also start(), restart(), hasExpired(), and 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:

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

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

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() and elapsed().

void QElapsedTimer::start()

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(), and elapsed().

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

Returns true if this object and other contain different times.

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

Returns true if this object and other contain the same time.

Related Non-Members

booloperator<(const QElapsedTimer &v1, const QElapsedTimer &v2)

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

© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.