第一种方法:
dwOldTime = GetTickCount(); DoSomething(); dwTimeElapsed = GetTickCount() – dwOldTime;
第二种方法:
class CStopwatch { public: CStopwatch() { QueryPerformanceFrequency(&m_liPerfFreq); Start(); } void Start() { QueryPerformanceCounter(&m_liPerfStart); } __int64 Now() const { //Returns # of milliseconds since //Start was called LARGE_INTEGER liPerfNow; QueryPerformanceCounter(&liPerfNow); return (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000)/ m_liPerfFreq.QuadPart); } private: //Counts per second LARGE_INTEGER m_liPerfFreq; //Starting count LARGE_INTEGER m_liPerfStart; };
使用方法:
//Create a stopwatch timer //(which defaults to the current time). CStopwatch stopwatch; //Execute the code I want to profile here. //Get how much time has elapsed up to now. __int64 qwElapsedTime = stopwatch.Now(); //qwElapsedTime indicates how long //the profiled code executed in milliseconds.
QueryPerformanceFrequency() - 基本介绍
类型:Win32API
原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
作用:返回硬件支持的高精度计数器的频率。
返回值:非零,硬件支持高精度计数器;零,硬件不支持,读取失败。
供WIN9X使用的高精度定时器:QueryPerformanceFrequency()和QueryPerformanceCounter(),要求计算机从硬件上支持高精度定时器。
函数的原形是:
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
BOOL QueryPerformanceCounter (LARGE_INTEGER *lpCount);
数据类型LARGEINTEGER既可以是一个作为8字节长的整数,也可以是作为两个4字节长的整数的联合结构,其具体用法根据编译器是否支持64位而定。该类型的定义如下:
typeef union _ LARGE_INTEGER
{
struct
{
DWORD LowPart;
LONG HighPart;
};
LONGLONG QuadPart;
} LARGE_INTEGER;
在定时前应该先调用QueryPerformanceFrequency()函数获得机器内部计时器的时钟频率。接着在需要严格计时的事件发生前和发生之后分别调用QueryPerformanceCounter(),利用两次获得的计数之差和时钟频率,就可以计算出事件经历的精确时间。测试函数SLEEP(100)的精确持续时间方法:
LARGE_INTEGER litmp;
LONGLONG qt1,qt2;
double dft,dff,dfm;
QueryPerformanceFrequency(&litmp);//获得时钟频率
dff=(double)litmp.QuadPart;
QueryPerformanceCounter(&litmp);//获得初始值
qt1=litmp.QuadPart;Sleep(100);
QueryPerformanceCounter(&litmp);//获得终止值
qt2=litmp.QuadPart;
dfm=(double)(qt2-qt1);
dft=dfm/dff;//获得对应的时间值
需要注意的是DFT计算的结果单位是秒。
第三种方法:
__int64 FileTimeToQuadWord(PFILETIME pft) { return (Int64ShllMod32( pft->dwHighDateTime, 32) | pft->dwLowDateTime); } void PerformLongOperation() { FILETIME ftKernelTimeStart, ftKernelTimeEnd; FILETIME ftUserTimeStart, ftUserTimeEnd; FILETIME ftDummy; __int64 qwKernelTimeElapsed, qwUserTimeElapsed, qwTotalTimeElapsed; //Get starting times. GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeStart, &ftUserTimeStart); //Perform complex algorithm here. //Get ending times. GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeEnd, &ftUserTimeEnd); //Get the elapsed kernel and user times by //converting the start and end times //from FILETIMEs to quad words, and then //subtract the start times from the end times. qwKernelTimeElapsed = FileTimeToQuadWord(&ftKernelTimeEnd) - FileTimeToQuadWord(&ftKernelTimeStart); qwUserTimeElapsed = FileTimeToQuadWord(&ftUserTimeEnd) - FileTimeToQuadWord(&ftUserTimeStart); //Get total time duration by adding the kernel //and user times. qwTotalTimeElapsed = qwKernelTimeElapsed + qwUserTimeElapsed; //The total elapsed time is in //qwTotalTimeElapsed. }