delphi time

DELPHI highly accurate timing method

// take time in milliseconds accuracy (Method a): 
  var 
    T1, T2: Int64; 
    R1: Int64; 
  the begin 
    T1: = the GetTickCount; // Get the API to start counting the WINDOWS 
    SLEEP ( 1000 ); { do ... } // perform to the timing code 
    T2: = the GetTickCount; // Get the end of the count value 
    R1: = T2-T1; // acquisition timer time, Y \ `~ 4 country network (} .u_% t" hV milliseconds (MS) 
    ShowMessage ( IntToStr (R1));
  End ; 

  // get time in milliseconds accuracy (method II): 
  // use DateUtils; // reference DateUtils units 
  var 
    T1, T2: TDateTime; 
    R1: Int64; 
  the begin 
    T1: = now (); / / acquisition start counting time
    SLEEP ( 1000 ); { do ... } // execute code to the timing 
    T2: = now (); // Get finishes counting time 
    R1: = SecondsBetween (T2, T1); // obtain timing period, D6k = + TsoUbP _II incubated in seconds W is (S) 
    R1: = MilliSecondsBetween (T2, T1); // acquisition time of time, 

  in milliseconds (MS) 
    ShowMessage (IntToStr (R1)); 
  End ; 

  // NOTE: the above two methods I tested produced by the timing accuracy seems only 0.01 seconds 

  @ take system time accuracy level: 
  var 
    C1: Int64; 
    T1, T2: Int64; 
    R1: Double; 
  the begin 
    QueryPerformanceFrequency (C1); // the WINDOWS the API returns a count of the frequency (Intel86 : 1,193,180) (the number of vibration frequency counter to obtain system performance within a millisecond)
    The QueryPerformanceCounter (T1); // the WINDOWS the API acquired start count value 
    SLEEP ( 1000 ); { do ... } // perform the timing code to 
    the QueryPerformanceCounter (T2); // Get the end of the count value 
    r1: = (t2-t1) / C1; // acquisition time of time, 


  in seconds (S) 
    R1: = (T2-T1) / C1 * 1000 ; // acquisition time of time, in milliseconds (MS) 
    R1: = (T2-T1) / C1 * 1000000 ; // acquisition time of time, in microseconds 
    ShowMessage (floattostr (R1));
  End ;
View Code

delphi delay function

//延迟函数:方法一
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 
    The CloseHandle (the Event); 
End ; 



  
// Delay function: Method II 
Procedure Delay (dwMilliseconds: DWORD); // Longint 
var 
iStart, ISTOP: DWORD; 
the begin 
    iStart: =    the GetTickCount;
     REPEAT 
    ISTOP: =    the GetTickCount; 
    Application.ProcessMessages by; 
    an until ( ISTOP - iStart)> = dwMilliseconds;
 End ; 


method three 
Procedure delay (for msecs: Integer); // delay sleep function better than 
var 
the Tick: DWord; 
the Event: THandle; 
the begin 
the Event: = the 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

A piece of code execution time consumed

The key is to use a library function GetTickCount 

[GetTickCount function name] 

[] Return Value Long, in milliseconds, 

is typically used to calculate the time an operation used: 
var : 

    START_TIME: a LONG; 

    stop_time: a LONG; 

the begin : 
START_TIME: = the GetTickCount; 

call interface operation 
stop_time: = the GetTickCount; 

the ShowMessage (the IntToStr (stop_time - art_time));
 End ;
View Code

Get System Time

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 (the DateTime);
 // Label1.Caption: = I; 
// Label1.Caption: the FormatDateTime = ( 'zz NN ss a time hh yyyy mm-dd-day', now ()); 
// The GetSystemTime () ; 
// The GetLocalTime (CT); 
// Label1.Caption: The GetSystemTime = (CT); // the FormatDateTime ( 'YYYY on January mm nn minutes and seconds dd day hh ss ZZ', The GetSystemTime (CT)); 
End ;
View Code

 

Guess you like

Origin www.cnblogs.com/blogpro/p/11345349.html