Get time of execution piece of code

感情迁移 提交于 2019-12-10 10:42:47

问题


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


回答1:


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.




回答2:


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




回答3:


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;


来源:https://stackoverflow.com/questions/2697995/get-time-of-execution-piece-of-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!