delphi 时间

DELPHI高精度计时方法

//取毫秒级时间精度(方法一):
  var
    t1,t2:int64;
    r1:int64;
  begin
    t1:=GetTickCount;//获取开始计数 WINDOWS API
    sleep(1000);{do...}//执行要计时的代码
    t2:=GetTickCount;//获取结束计数值
    r1:=t2-t1;//取得计时时间,Y\`国4~络(}.u_%t"hV单位毫秒(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);//取得计时时间,D6k=+W的TsoUbP育_II单位秒(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;
View Code

delphi 延迟函数

//延迟函数:方法一
procedure delay(msecs:integer);
var 
  Tick: DWord; 
  Event: THandle; 
begin 
  Event := CreateEvent(nil, False, False, nil); 
  try 
    Tick := GetTickCount + DWord(msecs); 
    while (msecs > 0) and (MsgWaitForMultipleObjects(1, Event, False, msecs, QS_ALLINPUT) <> WAIT_TIMEOUT) do 
    begin 
      Application.ProcessMessages; 
      msecs := Tick - GetTickcount; 
    end; 
  finally 
    CloseHandle(Event); 
end; 



  
//延迟函数:方法二
procedure Delay(dwMilliseconds:DWORD);//Longint
var
iStart,iStop:DWORD;
begin
    iStart :=   GetTickCount;
    repeat
    iStop  :=   GetTickCount;
    Application.ProcessMessages;
    until (iStop  -  iStart) >= dwMilliseconds;
end;


方法三
procedure delay(msecs:integer); //延迟函数 比sleep好
var
Tick: DWord;
Event: THandle;
begin
Event := CreateEvent(nil, False, False, nil);
try
Tick := GetTickCount + DWord(msecs);
while (msecs > 0) and (MsgWaitForMultipleObjects(1, Event, False, msecs, QS_ALLINPUT) <> WAIT_TIMEOUT) do
begin
Application.ProcessMessages;
msecs := Tick - GetTickcount;
end;
finally
CloseHandle(Event);
end;
View Code

一段代码执行消耗的时间

 关键是用到一个库函数GetTickCount

【函数名】 GetTickCount

【返回值】 Long,以毫秒为单位

通常用来计算某个操作所使用的时间: 
var:

    start_time : LONG;

    stop_time:LONG;

begin:
start_time :=GetTickCount;

调用接口操作
stop_time :=GetTickCount;

ShowMessage( IntToStr(stop_time - art_time));
end;
View Code

获取系统时间

var
DateTime:TDateTime;
i:string;
begin
DateTime:=now;
i:=DateToStr(DateTime)+' '+TimeToStr (DateTime);
lable1.caption:=i;


FormatDateTime('hh:nn:ss',Now());
FormatDateTime('yyyy年mm月dd日 hh时nn分ss秒zzzz',now());

var
DateTime:TDateTime;
i:string;
ct:TSystemTime;
begin
DateTime:=now;
i:=DateToStr(DateTime)+' '+TimeToStr (DateTime);
//Label1.caption:=i;
//Label1.caption:=FormatDateTime('yyyy年mm月dd日 hh时nn分ss秒zz',now());
//GetSystemTime();
//GetLocalTime(ct);
//Label1.caption:=GetSystemTime(ct);//FormatDateTime('yyyy年mm月dd日 hh时nn分ss秒zz',GetSystemTime(ct));
end;
View Code

 

转载于:https://www.cnblogs.com/blogpro/p/11345349.html

你可能感兴趣的:(delphi 时间)