/***用高精度计时器,毫秒为单位***/
#include <iostream>
#include <windows.h>
using namespace std ;
int main(void)
{
LARGE_INTEGER BegainTime ;
LARGE_INTEGER EndTime ;
LARGE_INTEGER Frequency ;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&BegainTime) ;
//要测试的代码放在这里
QueryPerformanceCounter(&EndTime) ;
cout << ( EndTime.QuadPart - BegainTime.QuadPart )*1000 / Frequency.QuadPart ;
system("pause") ;
return 0 ;
}
以下微秒为单位
#include <iostream>
#include <Windows.h>
using namespace std;
class timer
{
/***微秒为单位***/
public:
timer()
{
QueryPerformanceFrequency(&m_i64CPUFreq);
}
~timer()
{
}
void start(void)
{
reset();
QueryPerformanceCounter(&m_i64Begin);
}
void end(void)
{
QueryPerformanceCounter(&m_i64End);
}
DWORD getTime(void) const
{
DWORD dwTotalMicrosecond = 0;
LARGE_INTEGER i64TickCount;
i64TickCount.QuadPart = m_i64End.QuadPart - m_i64Begin.QuadPart;
dwTotalMicrosecond = (DWORD) (i64TickCount.QuadPart * 1000000 / m_i64CPUFreq.QuadPart);
return dwTotalMicrosecond;
}
private:
LARGE_INTEGER m_i64CPUFreq;
LARGE_INTEGER m_i64Begin;
LARGE_INTEGER m_i64End;
void reset(void)
{
m_i64Begin = m_i64End;
}
};
/***测试例子***/
int main()
{
timer t;
t.start();
{
for(int i = 0; i < 1000000; i++)
{
int n = i;
}
}
t.end();
cout<<t.getTime()<<endl;
return 0;
}