定时器的时钟频率、CPU主频
//【直接上可以运行的代码】
【定时器的时钟频率、CPU主频】//定时器的时钟频率、CPU主频.cpp
//#include
#include
#include //包含函数WinExec、Sleep,类型UINT
inline unsigned __int64 GetCycleCount(void)
//获得开机后CPU到现在的运行周期数
{
//_asm_emit 0x0F
//_asm_emit 0x31
_asmrdtsc;
//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");
}
}
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量