Delphi高精度计数例程,主程序如下:
procedure TForm1.Button1Click(Sender: TObject);
//取系统级时间精度:
var
c1:int64;
t1,t2,t3,t4,t5:int64;
r1,r2,r3,r4:double;
begin
QueryPerformanceFrequency(c1);//WINDOWS API 返回计数频率(Intel86:1193180)(获得系统的高性能频率计数器在一毫秒内的震动次数)
QueryPerformanceCounter(t1);//WINDOWS API 获取开始计数值
sleep(1000);{do...}//执行要计时的代码
QueryPerformanceCounter(t2);//获取结束计数值
r1:=(t2-t1)/c1;//取得计时时间,单位秒(s)
Edit1.Text:=FloatToStr(r1);
Edit1.Refresh;
sleep(1000);{do...}//执行要计时的代码
QueryPerformanceCounter(t3);//获取结束计数值
r2:=(t3-t2)/c1;//取得计时时间,单位秒(s)
Edit2.Text:=FloatToStr(r2);
Edit2.Refresh;
sleep(1000);{do...}//执行要计时的代码
QueryPerformanceCounter(t4);//获取结束计数值
r3:=(t4-t3)/c1;//取得计时时间,单位秒(s)
Edit3.Text:=FloatToStr(r3);
Edit3.Refresh;
sleep(1000);{do...}//执行要计时的代码
QueryPerformanceCounter(t5);//获取结束计数值
r4:=(t5-t4)/c1;//取得计时时间,单位秒(s)
Edit4.Text:=FloatToStr(r4);
Edit4.Refresh;
//r1:=(t2-t1)/c1*1000;//取得计时时间,单位毫秒(ms)
//r1:=(t2-t1)/c1*1000000;//取得计时时间,单位微秒
//showmessage(floattostr(r1));
end;