定时器的时钟频率、CPU主频

//【直接上可以运行的代码】

//定时器的时钟频率、CPU主频.cpp


//#include
#include
#include //包含函数WinExec、Sleep,类型UINT
inline unsigned __int64 GetCycleCount(void)
//获得开机后CPU到现在的运行周期数
{
//_asm     _emit 0x0F
//_asm     _emit 0x31
_asm     rdtsc;      //Read Time-Stamp Counter等价于上两句【汇编指令】
}
#define testTime0 1000




void main()
{
LARGE_INTEGER litmp0,litmp1,litmp2;//lastTime;
for (int i=0;i<=100;i++)
{
QueryPerformanceFrequency(&litmp0);//本机内部定时器的时钟频率
long cpuFreq=litmp0.QuadPart;
//printf("本机内部定时器的时钟频率=%u\n", cpuFreq);
QueryPerformanceCounter(&litmp1);
Sleep(1000);
QueryPerformanceCounter(&litmp2);
double beginTime,endTime,lastTime;
beginTime=1000*litmp1.QuadPart/cpuFreq;
endTime=1000*litmp2.QuadPart/cpuFreq;
lastTime=endTime-beginTime;
//printf("计时起点值=%f(ms)\n",beginTime);
//printf("计时终点值=%f(ms)\n",endTime);
//printf("间隔时间=%f(ms)\n",lastTime);
printf("计时误差=%f(ms)\n",lastTime-1000);


printf("\n");
unsigned __int64 beginCycleCount=GetCycleCount();//获得开机后CPU到现在的运行周期数
Sleep(testTime0);
unsigned __int64 endCycleCount=GetCycleCount();
signed __int64 cpuFrequency=endCycleCount-beginCycleCount;
//printf("CPU主频=%I64u\n",cpuFrequency);
signed __int64 lastCycleCount;

beginCycleCount=GetCycleCount();//获得开机后CPU到现在的运行周期数
Sleep(1000);
endCycleCount=GetCycleCount();

lastCycleCount=endCycleCount-beginCycleCount;
lastTime=lastCycleCount/(double)cpuFrequency*1000;
//printf("计时起点值=%f(ms)\n",1000*(signed __int64)beginCycleCount/(double)cpuFrequency);
//printf("计时终点值=%f(ms)\n",1000*(signed __int64)endCycleCount/(double)cpuFrequency);
//printf("间隔时间=%.15f(ms)\n",lastTime);
printf("计时误差=%f(ms)\n",lastTime-1000);


printf("\n");
beginTime=1000*(double)(signed __int64)beginCycleCount/(double)cpuFrequency;
endTime=1000*(double)(signed __int64)endCycleCount/(double)cpuFrequency;
lastTime=endTime-beginTime;
//printf("计时起点值=%f(ms)\n",beginTime);
//printf("计时终点值=%f(ms)\n",endTime);
//printf("间隔时间=%.15f(ms)\n",lastTime);
printf("计时误差=%f(ms)\n",lastTime-1000);


printf("\n");
printf("\n");
}


}







你可能感兴趣的:(定时器的时钟频率、CPU主频)