DELPHI高精度計時方法

來源:互聯網
上載者:User

//取毫秒級時間精度(方法一):
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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.