提取线程执行的有效时间,精确到100ns,摘自windows核心编程。 // 定义全局函数,用以转换 unsigned __int64 FileTimeToQuadWord(PFILETIME pft) { return (Int64ShllMod32(Int64ShllMod32(pft->dwHighDateTime, 31), 1) | pft->dwLowDateTime); } 计时代码执行时间 FILETIME ftKernelTimeStart, ftKernelTimeEnd; FILETIME ftUserTimeStart, ftUserTimeEnd; FILETIME ftDummy; unsigned __int64 qwKernelTimeElapsed, qwUserTimeElapsed; // get starting times GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeStart, &ftUserTimeStart); // 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;
直接给俺在2楼的回复无视了 这里给段代码,摘自msdn// crt_clock.c /* This example prompts for how long * the program is to run and then continuously * displays the elapsed time for that period. */#include <stdio.h> #include <stdlib.h> #include <time.h>void sleep( clock_t wait );int main( void ) { long i = 6000000L; clock_t start, finish; double duration; /* Delay for a specified time. */ printf( "Delay for three seconds\n" ); sleep( (clock_t)3 * CLOCKS_PER_SEC ); printf( "Done!\n" ); /* Measure the duration of an event. */ printf( "Time to do %ld empty loops is ", i ); start = clock(); while( i-- ) ; finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf( "%2.1f seconds\n", duration ); }/* Pauses for a specified number of milliseconds. */ void sleep( clock_t wait ) { clock_t goal; goal = wait + clock(); while( goal > clock() ) ; }
使用:LARGE_INTEGER m_nFreq;
LARGE_INTEGER m_nBeginTime;
LARGE_INTEGER nEndTime;
QueryPerformanceFrequency(&m_nFreq); // 获取时钟周期
QueryPerformanceCounter(&m_nBeginTime); // 获取时钟计数
Sleep(100);
QueryPerformanceCounter(&nEndTime);
cout << (nEndTime.QuadPart-m_nBeginTime.QuadPart)*1000/m_nFreq.QuadPart << endl;
原理:CPU上也有一个计数器,以机器的clock为单位,可以通过rdtsc读取,而不用中断,因此其精度与系统时间相当。
精度:计算机获取硬件支持,精度比较高,可以通过它判断其他时间函数的精度范围。
{
__asm _emit 0x0F
__asm _emit 0x31
}
以后在需要计数器的场合,可以像使用普通的Win32 API一样,调用两次GetCycleCount函数,比较两个返回值的差,像这样:
unsigned long t;
t = (unsigned long)GetCycleCount();
//Do Something time-intensive ...
t -= (unsigned long)GetCycleCount();
{
public:
CStopWatch()
{
QueryPerformanceFrequency(&perfFreq_);
//TRACE1("CStopWatch::CStopWatch(): perfFreq_.QuadPart = %u.\n", perfFreq_.QuadPart);
Start();
} void Start()
{
QueryPerformanceCounter(&perfStart_);
//TRACE1("CStopWatch::Start(): perfStart_.QuadPart = %u.\n", perfStart_.QuadPart);
} __int64 Now() const
{
LARGE_INTEGER perfNow;
QueryPerformanceCounter(&perfNow);
//TRACE1("CStopWatch::Now(): perfNow.QuadPart = %u.\n", perfNow.QuadPart); // @返回毫秒值
return(((perfNow.QuadPart - perfStart_.QuadPart) * 1000) / perfFreq_.QuadPart);
} __int64 NowInMicro() const
{
LARGE_INTEGER perfNow;
QueryPerformanceCounter(&perfNow);
//TRACE1("CStopWatch::NowInMicro(): perfNow.QuadPart = %u.\n", perfNow.QuadPart); // @返回毫微值
return(((perfNow.QuadPart - perfStart_.QuadPart) * 1000000) / perfFreq_.QuadPart);
}private:
LARGE_INTEGER perfFreq_;
LARGE_INTEGER perfStart_;
};
可用类,现成的
// 定义全局函数,用以转换
unsigned __int64 FileTimeToQuadWord(PFILETIME pft) {
return (Int64ShllMod32(Int64ShllMod32(pft->dwHighDateTime, 31), 1) | pft->dwLowDateTime);
}
计时代码执行时间 FILETIME ftKernelTimeStart, ftKernelTimeEnd;
FILETIME ftUserTimeStart, ftUserTimeEnd;
FILETIME ftDummy;
unsigned __int64 qwKernelTimeElapsed, qwUserTimeElapsed; // get starting times
GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy,
&ftKernelTimeStart, &ftUserTimeStart); // 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;
{
_asm _emit 0x0f
_asm _emit 0x31
}class KTimer
{
unsigned __int64 m_startcycle;
public:
unsigned __int64 m_overhead; KTimer()
{
m_overhead = 0;
Start();
m_overhead = Stop();
} void Start()
{
m_startcycle = GetCycleCount();
} unsigned __int64 Stop()
{
return GetCycleCount() - m_startcycle - m_overhead;
}
};void showCpuSpeed()
{
KTimer timer;
timer.Start();
Sleep(2000);
unsigned __int64 cpuspeed10 = timer.Stop() / 200000;
printf("CPU Speed : %d.%d MHz\n", cpuspeed10 / 10, cpuspeed10 % 10);
}
用这段函数试试
http://blog.daviesliu.net/2004/07/29/041050/
...
RDTSC->_End(_End-_Start)*1000/(2*2<<32)结果始终为0ms(此处假设CPU频率为2GHz);
也就是说CPU频率越快,其计算结果为0的可能性越大,对吗?
这里给段代码,摘自msdn// crt_clock.c
/* This example prompts for how long
* the program is to run and then continuously
* displays the elapsed time for that period.
*/#include <stdio.h>
#include <stdlib.h>
#include <time.h>void sleep( clock_t wait );int main( void )
{
long i = 6000000L;
clock_t start, finish;
double duration; /* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" ); /* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
不过计算结果还是0.
LARGE_INTEGER Start_Count;
if(! QueryPerformanceCounter( &Start_Count ))
{
TRACE(0);
} 这里加入测试代码/调用函数 如Sleep(1000); //stop
LARGE_INTEGER Stop_Count;
if(! QueryPerformanceCounter( &Stop_Count ) )
{
TRACE(0);
} //consume
LARGE_INTEGER Consume_Count;
Consume_Count.QuadPart = Stop_Count.QuadPart - Start_Count.QuadPart; LARGE_INTEGER PerformanceFreq;
if(!QueryPerformanceFrequency( &PerformanceFreq ) )
{
TRACE(0);
}
//consume times in ms
double msTimes = (double)(Consume_Count.QuadPart) / (double)(PerformanceFreq.QuadPart);
for (i = 0; i < 1000; i++)
CallFunc(...);
dwEnd = GetTickCount();printf("CallFunc time: %d/1000 ms\n", dwEnd - dwBegin);