How do I get milliseconds time of execution of a piece of code in Qt/C++?
3 Answers
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.
Comments
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
Comments
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;