尽管是WINDOWS下的程序,我还是在CU上贴出来。欢迎LINUX下相同程序的跟贴以及板砖。
//=====================================================================
#include <windows.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
LARGE_INTEGER sLARGE_INTEGER_Frequency;
LARGE_INTEGER sLARGE_INTEGER_Start;
LARGE_INTEGER sLARGE_INTEGER_End;
sLARGE_INTEGER_Frequency.QuadPart = 0;
sLARGE_INTEGER_Start.QuadPart = 0;
sLARGE_INTEGER_End.QuadPart = 0;
printf( "QueryPerformanceCounter():\n" );
BOOL bSupport;
bSupport = QueryPerformanceFrequency( &sLARGE_INTEGER_Frequency );
if( bSupport )
{
BOOL bSuccessStart;
BOOL bSuccessEnd;
bSuccessStart = QueryPerformanceCounter( &sLARGE_INTEGER_Start );
//printf( "How long has I consumed?\n" );
Sleep( 40 );
bSuccessEnd = QueryPerformanceCounter( &sLARGE_INTEGER_End );
if( bSuccessStart && bSuccessEnd )
{
LONGLONG llConsume_Milliseconds = ( sLARGE_INTEGER_End.QuadPart - sLARGE_INTEGER_Start.QuadPart ) * 1000 / sLARGE_INTEGER_Frequency.QuadPart;
LONGLONG llConsume_Ticks = ( sLARGE_INTEGER_End.QuadPart - sLARGE_INTEGER_Start.QuadPart );
printf( "Start: %ld\n", sLARGE_INTEGER_Start.QuadPart );
printf( "End: %ld\n", sLARGE_INTEGER_End.QuadPart );
printf( "Consume: %ld ticks\n", llConsume_Ticks );
printf( "Consume: %ld milliseconds\n", llConsume_Milliseconds );
printf( "Minimum resolution: 1 / %ld second\n", sLARGE_INTEGER_Frequency.QuadPart );
}
else
{
printf( "Do not successed!\n" );
}
}
else
{
printf( "Do not support high-resolution performance counter!\n" );
}
getchar();
printf( "//=====================================================================\n" );
DWORD dwStart = GetTickCount();
printf( "GetTickCount(): %lu milliseconds\n", dwStart );
getchar();
return 0;
}
下面是更简洁的函数和使用用例:
//======================================================
//timer.h
//======================================================
#ifndef __TIMER_H__
#define __TIMER_H__
double Timer( void );
void StartTimer( void );
double EndTimer( void );
double CpuTimer( void );
__int64 CpuTimeStamp( void );
#endif __TIMER_H__
//======================================================
//timer.cpp
//======================================================
#include "timer.h"
#include <windows.h>
static double g_Timer = 0.0;
double Timer( void )
{
static double s = 0.0;
LARGE_INTEGER c, f;
QueryPerformanceCounter( &c );
QueryPerformanceFrequency( &f );
if( s == 0.0 )
s = (double) c.QuadPart / (double) f.QuadPart;
return (double) c.QuadPart / (double) f.QuadPart - s;
}
void StartTimer()
{
g_Timer = Timer();
}
double EndTimer( void )
{
return Timer() - g_Timer;
}
double CpuTimer( void )
{
__int64 timer;
__asm
{
rdtsc
mov DWORD PTR timer+0, eax;
mov DWORD PTR timer+4, edx;
}
return (double) timer;
}
__int64 CpuTimeStamp( void )
{
__int64 c;
__asm
{
rdtsc
mov DWORD PTR c+0, eax
mov DWORD PTR c+4, edx;
}
return c;
}
//======================================================
// main.cpp
//======================================================
#include <stdio.h>
#include <windows.h>
#include "timer.h"
int main(int argc, char* argv[])
{
StartTimer();
Sleep( 1000 );
double dEndTimer = EndTimer();
printf( "consume time = %f s\n", dEndTimer );
getchar();
return 0;
}
//=============================================================================
同时在
http://www.cppfans.com/articles/system/cpuspd_usdly.asp
有一个比较好的对CPU进行汇编测时的例子。下面是原文:
CPU 测速(MHz)和高精度延时(微秒级) Victor Chen, (C++爱好者)
一.高精度延时, 是 CPU 测速的基础
Windows 内部有一个精度非常高的定时器, 精度在微秒级, 但不同的系统这个定时器的频率不同, 这个频率与硬件和操作系统都可能有关。
利用 API 函数 QueryPerformanceFrequency 可以得到这个定时器的频率。
利用 API 函数 QueryPerformanceCounter 可以得到定时器的当前值。
根据要延时的时间和定时器的频率, 可以算出要延时的时间定时器经过的周期数。
在循环里用 QueryPerformanceCounter 不停的读出定时器值, 一直到经过了指定周期数再结束循环, 就达到了高精度延时的目的。
高精度延时的程序, 参数: 微秒:
void DelayUs(__int64 Us)
{
LARGE_INTEGER CurrTicks, TicksCount;
QueryPerformanceFrequency(&TicksCount);
QueryPerformanceCounter(&CurrTicks);
TicksCount.QuadPart = TicksCount.QuadPart * Us / 1000000i64;
TicksCount.QuadPart += CurrTicks.QuadPart;
while(CurrTicks.QuadPart<TicksCount.QuadPart)
QueryPerformanceCounter(&CurrTicks);
}
二.测速程序
利用 rdtsc 汇编指令可以得到 CPU 内部定时器的值, 每经过一个 CPU 周期, 这个定时器就加一。
如果在一段时间内数得 CPU 的周期数, CPU工作频率 = 周期数 / 时间
为了不让其他进程和线程打扰, 必需要设置最高的优先级
以下函数设置当前进程和线程到最高的优先级。
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
CPU 测速程序的源代码, 这个程序通过 CPU 在 1/16 秒的时间内经过的周期数计算出工作频率, 单位 MHz:
int CPU_Frequency(void) //MHz
{
LARGE_INTEGER CurrTicks, TicksCount;
__int64 iStartCounter, iStopCounter;
DWORD dwOldProcessP = GetPriorityClass(GetCurrentProcess());
DWORD dwOldThreadP = GetThreadPriority(GetCurrentThread());
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
QueryPerformanceFrequency(&TicksCount);
QueryPerformanceCounter(&CurrTicks);
TicksCount.QuadPart /= 16;
TicksCount.QuadPart += CurrTicks.QuadPart;
asm rdtsc
asm mov DWORD PTR iStartCounter, EAX
asm mov DWORD PTR (iStartCounter+4), EDX
while(CurrTicks.QuadPart<TicksCount.QuadPart)
QueryPerformanceCounter(&CurrTicks);
asm rdtsc
asm mov DWORD PTR iStopCounter, EAX
asm mov DWORD PTR (iStopCounter + 4), EDX
SetThreadPriority(GetCurrentThread(), dwOldThreadP);
SetPriorityClass(GetCurrentProcess(), dwOldProcessP);
return (int)((iStopCounter-iStartCounter)/62500);
}
前面是用 API 函数进行延时, 如果知道了 CPU 的工作频率, 利用循环, 也可以得到高精度的延时
int _CPU_FREQ = 0; //定义一个全局变量保存 CPU 频率 (MHz)
void CpuDelayUs(__int64 Us) //利用循环和 CPU 的频率延时, 参数: 微秒
{
__int64 iCounter, iStopCounter;
asm rdtsc
asm mov DWORD PTR iCounter, EAX
asm mov DWORD PTR (iCounter+4), EDX
iStopCounter = iCounter + Us*_CPU_FREQ;
while(iStopCounter-iCounter>0)
{
asm rdtsc
asm mov DWORD PTR iCounter, EAX
asm mov DWORD PTR (iCounter+4), EDX
}
}
void TestDelay(void)
{
_CPU_FREQ = CPU_Frequency(); //利用 CPU 频率初始化定时
CpuDelayUs(1000000); //延时 1 秒钟
}
总结:
无论是前面程序中的 DelayUs 函数还是 CpuDelayUs 函数, 实际上的基础都是 API 函数 QueryPerformanceCounter
如果认为精度不够, 可以在测试 CPU 频率时延时更长时间, 但经过我的测试, 1/16秒足够了, 可以较高精度的测出 CPU 的工作频率。
程序里要进行高精度(微秒级)的延时, 完全可以采用 DelayUs 和 CpuDelayUs 函数。