使用QueryPerformanceFrequency 計算程式執行時間
阿新 • • 發佈:2019-02-15
QueryPerformanceFrequency( __out LARGE_INTEGER *lpFrequency ); //返回定時器的頻率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount); //是返回定時器當前計數值
程式碼:
///////////////////////////////////////////////// #include <iostream> #include <windows.h> using namespace std; //////////////////////////////////////////////// void main() { _LARGE_INTEGER time_start; /*開始時間*/ _LARGE_INTEGER time_over;/*結束時間*/ double dqFreq; /*計時器頻率*/ LARGE_INTEGER f; /*計時器頻率*/ QueryPerformanceFrequency(&f); dqFreq=(double)f.QuadPart; QueryPerformanceCounter(&time_start); Sleep(1000);/*迴圈耗時*/ QueryPerformanceCounter(&time_over); cout<<((time_over.QuadPart-time_start.QuadPart)/dqFreq)<<endl;//單位為秒,精度為1000 000/(cpu主頻)微秒 }