Regof
Regof

Reputation: 2857

Get time of execution piece of code

How do I get milliseconds time of execution of a piece of code in Qt/C++?

Upvotes: 2

Views: 1006

Answers (3)

photo_tom
photo_tom

Reputation: 7342

If you are running on a Windows system, then you can use timer based on the Windows Performace Timers and get microsecond timing.

Intel has a downloadable library at etimer libary. This is a small C routine that is fairly painless to use and gives very good results at the microsecond level

Upvotes: 4

Exa
Exa

Reputation: 4110

If you don't use Qt you can do it with a GetTickCount:

DWORD start = ::GetTickCount(); // start counter

// all the things your program does

DWORD end = ::GetTickCount(); // stop counter
DWORD duration = end - start;
std::cout << "Duration: "  << duration << " ms" << std::endl;

Upvotes: 1

Eli Bendersky
Eli Bendersky

Reputation: 273416

Use the QTime class. Start it with .start() (or .restart()) and then check the amount of milliseconds passed with .elapsed(). Naturally, the precision ultimately depends on the underlying OS, although with the major platforms you should have no trouble getting a real millisecond resolution.

Upvotes: 7

Related Questions