用下面的代码可以取代GetTickCount()计时,而且精确到微秒char asciiLine[20], sReply[100], smsg[200]; int initialized, ret, i; __int64 frequency,endtime,elapsed; __int64 begintime; BOOL qw; double welapsed;// start time memset(asciiLine,0,20); initialized = QueryPerformanceFrequency( (LARGE_INTEGER *)&frequency ); qw = QueryPerformanceCounter( (LARGE_INTEGER *)&begintime );// do some work ... // end work QueryPerformanceCounter( (LARGE_INTEGER *)&endtime ); // determine the elapsed counts elapsed = endtime - begintime; // convert counts to time in seconds and return it welapsed =(double)elapsed/ (double)frequency; dtoan(asciiLine,welapsed,10); // end time sprintf(smsg, "--------总用时:%s s--------", asciiLine);
int initialized, ret, i;
__int64 frequency,endtime,elapsed;
__int64 begintime;
BOOL qw;
double welapsed;// start time
memset(asciiLine,0,20);
initialized = QueryPerformanceFrequency( (LARGE_INTEGER *)&frequency );
qw = QueryPerformanceCounter( (LARGE_INTEGER *)&begintime );// do some work
...
// end work
QueryPerformanceCounter( (LARGE_INTEGER *)&endtime );
// determine the elapsed counts
elapsed = endtime - begintime;
// convert counts to time in seconds and return it
welapsed =(double)elapsed/ (double)frequency;
dtoan(asciiLine,welapsed,10);
// end time
sprintf(smsg, "--------总用时:%s s--------", asciiLine);
void dtoan(char *alpha,double val,int len)
{
char format[16];
int exp,i;
unsigned int lenx;
char *temp;
/* do we need to use exponential */
if(val < 0.0) sprintf(format,"%7.2e",val);
else sprintf(format,"%8.2e",val);
exp=strlen(&format[7]);
exp=atoin(&format[7],exp);
if(exp+2 >len)
{ /* use e format */
lenx=len+7;
i=sizeof(char);
temp=(char near *)calloc(lenx,i);
if(val < 0.0) sprintf(format,"%c%d.%de",'%',len-1,len-5);
else sprintf(format,"%c%d.%de",'%',len,len-4);
sprintf(temp,format,val);
lenx=strlen(temp);
i=lenx-2;
do {
if(temp[i]=='0') strcpy(&temp[i],&temp[i+1]);
else i--;
} while(i>0 && temp[i]!='e');
if(temp[i]=='e') strcpy(&temp[i],&temp[i+1]);
while (len < (int) strlen(temp))
{
i--;
strcpy(&temp[i],&temp[i+1]);
}
strcpy(alpha,temp);
free(temp);
}
else
{ /* use f format */
if(val < 0.0) sprintf(format,"%c%d.%df",'%',len-1,len-3-exp);
else sprintf(format,"%c%d.%df",'%',len,len-2-exp);
sprintf(alpha,format,val);
}
return;
}int atoin(char *alpha,int n)
{
int val;
char format[32];
char string[32];
sprintf(format,"%c%dd",'%',n);
val=0;
if(n>32)n=32;
strncpy(string,alpha,n);
string[n]='\0';
sscanf(string,format,&val);
return (val);
}
非常感谢您的回答,但是这个方法仍然无法避免《变速齿轮》的影响,希望您能帮忙处理一下。 谢谢!
来实现!
可是我汇编不熟,帮不了你你可以试试去INTEL下载4部3卷INTEL的指令白皮书来看看,
AMD的网站上还有可执行的C程序代码可以告诉你,
但是我不知道是不是通用的
{
__asm RDTSC
}但是不行,因为RDTSC不被C++的内嵌汇编器直接支持,所以我们要用_emit伪指令直接嵌入该指令的机器码形式0X0F、0X31,如下:inline unsigned __int64 GetCycleCount()
{
__asm _emit 0x0F
__asm _emit 0x31
}以后在需要计数器的场合,可以像使用普通的Win32 API一样,调用两次GetCycleCount函数,比较两个返回值的差,像这样:unsigned long t;
t = (unsigned long)GetCycleCount();
//Do Something time-intensive ...
t -= (unsigned long)GetCycleCount();《Windows图形编程》第15页编写了一个类,把这个计数器封装起来。有兴趣的读者可以去参考那个类的代码。作者为了更精确的定时,做了一点小小的改进,把执行RDTSC指令的时间,通过连续两次调用GetCycleCount函数计算出来并保存了起来,以后每次计时结束后,都从实际得到的计数中减掉这一小段时间,以得到更准确的计时数字。但我个人觉得这一点点改进意义不大。在我的机器上实测,这条指令大概花掉了几十到100多个周期,在Celeron 800MHz的机器上,这不过是十分之一微秒的时间。对大多数应用来说,这点时间完全可以忽略不计;而对那些确实要精确到纳秒数量级的应用来说,这个补偿也过于粗糙了。这个方法的优点是:
1.高精度。可以直接达到纳秒级的计时精度(在1GHz的CPU上每个时钟周期就是一纳秒),这是其他计时方法所难以企及的。2.成本低。timeGetTime 函数需要链接多媒体库winmm.lib,QueryPerformance* 函数根据MSDN的说明,需要硬件的支持(虽然我还没有见过不支持的机器)和KERNEL库的支持,所以二者都只能在Windows平台下使用(关于DOS平台下的高精度计时问题,可以参考《图形程序开发人员指南》,里面有关于控制定时器8253的详细说明)。但RDTSC指令是一条CPU指令,凡是i386平台下Pentium以上的机器均支持,甚至没有平台的限制(我相信i386版本UNIX和Linux下这个方法同样适用,但没有条件试验),而且函数调用的开销是最小的。3.具有和CPU主频直接对应的速率关系。一个计数相当于1/(CPU主频Hz数)秒,这样只要知道了CPU的主频,可以直接计算出时间。这和QueryPerformanceCount不同,后者需要通过QueryPerformanceFrequency获取当前计数器每秒的计数次数才能换算成时间。这个方法的缺点是:
1.现有的C/C++编译器多数不直接支持使用RDTSC指令,需要用直接嵌入机器码的方式编程,比较麻烦。
2.数据抖动比较厉害。其实对任何计量手段而言,精度和稳定性永远是一对矛盾。如果用低精度的
timeGetTime来计时,基本上每次计时的结果都是相同的;而RDTSC指令每次结果都不一样,经常有几百甚至上千的差距。这是这种方法高精度本身固有的矛盾。关于这个方法计时的最大长度,我们可以简单的用下列公式计算:自CPU上电以来的秒数 = RDTSC读出的周期数 / CPU主频速率(Hz)64位无符号整数所能表达的最大数字是1.8×10^19,在我的Celeron 800上可以计时大约700年(书中说可以在200MHz的Pentium上计时117年,这个数字不知道是怎么得出来的,与我的计算有出入)。无论如何,我们大可不必关心溢出的问题。下面是几个小例子,简要比较了三种计时方法的用法与精度
//Timer1.cpp 使用了RDTSC指令的Timer类//KTimer类的定义可以参见《Windows图形编程》P15
//编译行:CL Timer1.cpp /link USER32.lib
#include
#include "KTimer.h"
main()
{
unsigned t;
KTimer timer;
timer.Start();
Sleep(1000);
t = timer.Stop();
printf("Lasting Time: %d\n",t);
}//Timer2.cpp 使用了timeGetTime函数
//需包含,但由于Windows头文件错综复杂的关系
//简单包含比较偷懒:)
//编译行:CL timer2.cpp /link winmm.lib
#include
#include main()
{
DWORD t1, t2;
t1 = timeGetTime();
Sleep(1000);
t2 = timeGetTime();
printf("Begin Time: %u\n", t1);
printf("End Time: %u\n", t2);
printf("Lasting Time: %u\n",(t2-t1));
}//Timer3.cpp 使用了QueryPerformanceCounter函数
//编译行:CL timer3.cpp /link KERNEl32.lib
#include
#include main()
{
LARGE_INTEGER t1, t2, tc;
QueryPerformanceFrequency(&tc);
printf("Frequency: %u\n", tc.QuadPart);
QueryPerformanceCounter(&t1);
Sleep(1000);
QueryPerformanceCounter(&t2);
printf("Begin Time: %u\n", t1.QuadPart);
printf("End Time: %u\n", t2.QuadPart);
printf("Lasting Time: %u\n",( t2.QuadPart- t1.QuadPart));
}////////////////////////////////////////////////
//以上三个示例程序都是测试1秒钟休眠所耗费的时间
file://测试环境:Celeron 800MHz / 256M SDRAM
// Windows 2000 Professional SP2
// Microsoft Visual C++ 6.0 SP5
////////////////////////////////////////////////
以下是Timer1的运行结果,使用的是高精度的RDTSC指令
Lasting Time: 804586872以下是Timer2的运行结果,使用的是最粗糙的timeGetTime API
Begin Time: 20254254
End Time: 20255255
Lasting Time: 1001以下是Timer3的运行结果,使用的是QueryPerformanceCount API
Frequency: 3579545
Begin Time: 3804729124
End Time: 3808298836
Lasting Time: 3569712古人说,触类旁通。从一本介绍图形编程的书上得到一个如此有用的实时处理知识,我感到非常高兴。有美不敢自专,希望大家和我一样喜欢这个轻便有效的计时器。参考资料:
[YUAN 2002]Feng Yuan 著,英宇工作室 译,Windows图形编程,机械工业出版社,2002.4.,P15-17>>下面这个竟然测出了CPU的主频率:
procedure TForm1.Button1Click(Sender: TObject);
var
a,b,c,d : int64;
begin
c := gettickcount;
a := cyclecount;
sleep(1000);
d := gettickcount;
b := cyclecount;
label1.caption := 'CPU: '+floattostr((b-a)/(d-c)*1000/1000000)+' MHZ';
end;在DELPHI下试了试,感觉不错。
如果用鼠标触发,时间为46周期,用回车键的话,一般需要160个周期,看来回车过后,CPU更忙些function cyclecount:int64;
asm
db $0f
db $31
end;procedure TForm1.Button1Click(Sender: TObject);
begin
caption:=inttostr(cyclecount-cyclecount);
end;
2000下,变速齿轮仅仅是HOOK API 98下只要你的软件比变速齿轮先运行,就可以监测中断是否被修改
2000下只要做到API不被hook就行了
谢谢!