bestlong 怕失憶論壇
標題:
Delphi 高精度計時方法
[打印本頁]
作者:
bestlong
時間:
2006-8-4 10:28
標題:
Delphi 高精度計時方法
//取毫秒級時間精度(方法一):
var
t1, t2: int64;
r1: int64;
begin
t1 := GetTickCount; //獲取開始計數 WINDOWS API
Sleep(1000); {do...} //執行要計時的代碼
t2 := GetTickCount; //獲取结束計數值
r1 := t2 - t1; //取得計時時間,單位毫秒(ms)
ShowMessage(inttostr(r1));
end;
//取毫秒級時間精度(方法二):
//use DateUtils; //引用DateUtils單位
var
t1, t2: TDatetime;
r1: int64;
begin
t1 := now(); //獲取開始計時時間
Sleep(1000); {do...}//執行要計時的代碼
t2 := now();//獲取结束計時時間
r1 := SecondsBetween(t2, t1); //取得計時時間,單位秒(s)
r1 := MilliSecondsBetween(t2, t1); //取得計時時間,單位毫秒(ms)
ShowMessage(inttostr(r1));
end;
//注:以上兩種方式經本人测試好像只能產生0.01秒的計時精度
//取系统級時間精度:
var
c1:int64;
t1,t2:int64;
r1:double;
begin
QueryPerformanceFrequency(c1); //WINDOWS API 返回計數频率(Intel86:1193180)(獲得系统的高性能頻率計數器在一毫秒内的震動次數)
QueryPerformanceCounter(t1); //WINDOWS API 獲取開始計數值
Sleep(1000); {do...}//執行要計時的代碼
QueryPerformanceCounter(t2); //獲取结束計數值
r1 := (t2 - t1) / c1; //取得計時時間,單位秒(s)
r1 := (t2 - t1) / c1 * 1000; //取得計時時間,單位毫秒(ms)
r1 := (t2 - t1) / c1 * 1000000; //取得計時時間,單位微秒
ShowMessage(FloatToStr(r1));
end;
複製代碼
歡迎光臨 bestlong 怕失憶論壇 (http://www.bestlong.idv.tw/)
Powered by Discuz! X1.5