//取毫秒級時間精度(方法一):
var
t1,t2:int64;
r1:int64;
begin
t1:=GetTickCount;//獲取開始計數(shù) WINDOWS API
sleep(1000);{do...}//執(zhí)行要計時的代碼
t2:=GetTickCount;//獲取結束計數(shù)值
r1:=t2-t1;//取得計時時間,單位毫秒(ms)
showmessage(inttostr(r1));
end;
//取毫秒級時間精度(方法二):
//use DateUtils;//引用DateUtils單位
var
t1,t2:tdatetime;
r1:int64;
begin
t1:=now();//獲取開始計時時間
sleep(1000);{do...}//執(zhí)行要計時的代碼
t2:=now();//獲取結束計時時間
r1:=SecondsBetween(t2,t1);//取得計時時間,單位秒(s)
r1:=MilliSecondsBetween(t2,t1);//取得計時時間,單位毫秒(ms)
showmessage(inttostr(r1));
end;
//注:以上兩種方式經(jīng)本人測試好像只能產(chǎn)生0.01秒的計時精度
//取系統(tǒng)級時間精度:
var
c1:int64;
t1,t2:int64;
r1:double;
begin
QueryPerformanceFrequency(c1);//WINDOWS API 返回計數(shù)頻率(Intel86:1193180)(獲得系統(tǒng)的高性能頻率計數(shù)器在一毫秒內(nèi)的震動次數(shù))
QueryPerformanceCounter(t1);//WINDOWS API 獲取開始計數(shù)值
sleep(1000);{do...}//執(zhí)行要計時的代碼
QueryPerformanceCounter(t2);//獲取結束計數(shù)值
r1:=(t2-t1)/c1;//取得計時時間,單位秒(s)
r1:=(t2-t1)/c1*1000;//取得計時時間,單位毫秒(ms)
r1:=(t2-t1)/c1*1000000;//取得計時時間,單位微秒
showmessage(floattostr(r1));
end;
新聞熱點
疑難解答
圖片精選