使用QueryPerformanceFrequency 计算程序执行时间
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主频)微秒 }