头文件
1 #include <Windows.h>
嵌入到代码中的计时语句
1 //timer
2 LARGE_INTEGER nStartCounter, nStopCounter;
3 QueryPerformanceCounter(&nStartCounter);//timer starts
4 //【需要计时的代码片段】
5 QueryPerformanceCounter(&nStopCounter);//timer ends
6 double nTime1 = getCostTime(nStartCounter, nStopCounter);
2 LARGE_INTEGER nStartCounter, nStopCounter;
3 QueryPerformanceCounter(&nStartCounter);//timer starts
4 //【需要计时的代码片段】
5 QueryPerformanceCounter(&nStopCounter);//timer ends
6 double nTime1 = getCostTime(nStartCounter, nStopCounter);
转换为标准单位second
1 double getCostTime(LARGE_INTEGER start, LARGE_INTEGER end)
2 {
3 double timeAccuracy = TIME_ACCURACY;
4 LARGE_INTEGER nFrequency;
5 QueryPerformanceFrequency(&nFrequency);
6 double time = (double)timeAccuracy * (end.QuadPart - start.QuadPart) / nFrequency.QuadPart;
7 return time / timeAccuracy;
8 }
2 {
3 double timeAccuracy = TIME_ACCURACY;
4 LARGE_INTEGER nFrequency;
5 QueryPerformanceFrequency(&nFrequency);
6 double time = (double)timeAccuracy * (end.QuadPart - start.QuadPart) / nFrequency.QuadPart;
7 return time / timeAccuracy;
8 }
时间精度说明
据考证是最精确的计时方法。
(Any doubt? STFW.)