Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get elapsed time in Qt

Tags:

c++

qt

I'm looking for the equivalent in Qt to GetTickCount()

Something that will allow me to measure the time it takes for a segment of code to run as in:

uint start = GetTickCount(); // do something.. uint timeItTook = GetTickCount() - start; 

any suggestions?

like image 345
shoosh Avatar asked Oct 28 '08 20:10

shoosh


People also ask

How do you use QTimer?

The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout() signal to the appropriate slots, and call start(). From then on, it will emit the timeout() signal at constant intervals.

How do I disable QElapsedTimer?

You can't stop QElapsedTimer , because there is no timer. When you call method start() , QElapsedTimer saves the current time. When elapsed, it gets current time again, and calculate difference.


2 Answers

I think it's probably better to use QElapsedTimer since that is why the class exists in the first place. It was introduced with Qt 4.7. Note that it is also immuned to system's clock time change.

Example usage:

#include <QDebug> #include <QElapsedTimer> ... ... QElapsedTimer timer; timer.start(); slowOperation();  // we want to measure the time of this slowOperation() qDebug() << timer.elapsed(); 
like image 88
sivabudh Avatar answered Nov 16 '22 00:11

sivabudh


How about QTime? Depending on your platform it should have 1 millisecond accuracy. Code would look something like this:

QTime myTimer; myTimer.start(); // do something.. int nMilliseconds = myTimer.elapsed(); 
like image 33
Dusty Campbell Avatar answered Nov 16 '22 01:11

Dusty Campbell