Thursday, August 27, 2020

High Performance Timer in Delphi - TStopWatch

Elite Timer in Delphi - TStopWatch For routine work area database applications, adding a solitary second to an errands execution time infrequently has any kind of effect to end clients - yet when you have to process a huge number of tree leaves or produce billions of one of a kind irregular numbers, speed-of-execution turns out to be increasingly significant. Timing Out Your Code In certain applications, exceptionally exact, high-accuracy time estimation strategies are significant and fortunately Delphi gives an elite counter to qualifyâ these times. Utilizing RTLs Now Function One choice uses the Now work. Presently, characterized in the SysUtils unit, restores the current framework date and time. A couple of lines of code measure passed time between the beginning and stop of some procedure: var  â start, stop, passed : TDateTime;​ start  â start : Now;  â //TimeOutThis();  â stop : Now;  â elapsed : stop - start; end; The Now work restores the current framework date and time that is exact up to 10 milliseconds (Windows NT and later) or 55 milliseconds (Windows 98). For exceptionally little stretches the exactness of Now is once in a while insufficient. Utilizing Windows API GetTickCount For much increasingly exact information, utilize the GetTickCount Windows API work. GetTickCount recovers the quantity of milliseconds that have passed since the framework was begun, yet the capacity just has the exactness of 1 ms and may not generally be precise if the PC stays fueled up for significant stretches of time. The passed time is put away as a DWORD (32-piece) esteem. Hence, the time will fold over to zero if Windows is run ceaselessly for 49.7 days. var  â start, stop, slipped by : cardinal; start  â start : GetTickCount;  â //TimeOutThis();  â stop : GetTickCount;  â elapsed : stop - start;/millisecondsend; GetTickCount is additionally restricted to the exactness of the framework clock (10/55 ms). High Precision Timing Out Your Code On the off chance that your PC underpins a high-goals execution counter, utilize the QueryPerformanceFrequency Windows API capacity to communicate the recurrence, in tallies every second. The estimation of the check is processor subordinate. The QueryPerformanceCounter work recovers the current estimation of the high-goals execution counter. By calling this capacity toward the start and end of a segment of code, an application utilizes the counter as a high-goals clock. The exactness of high-goals clocks is around a couple hundred nanoseconds. A nanosecond is a unit of time speaking to 0.000000001 seconds or 1 billionth of a second. TStopWatch: Delphi Implementation of a High-Resolution Counter With a gesture to .Net naming shows, a counter like TStopWatch offers a high-goals Delphi answer for exact time estimations. TStopWatch measures passed time by including clock ticks in the hidden clock component. The IsHighResolution property shows whether the clock depends on a high-goals execution counter.The Start strategy begins estimating slipped by time.The Stop technique quits estimating passed time.The ElapsedMilliseconds property gets the complete slipped by time in milliseconds.The Elapsed property gets the all out passed time in clock ticks. unit StopWatch;interface utilizes Windows, SysUtils, DateUtils;type TStopWatch class  â private     fFrequency : TLargeInteger;     fIsRunning: boolean;     fIsHighResolution: boolean;     fStartCount, fStopCount : TLargeInteger;  â â â procedure SetTickStamp(var lInt : TLargeInteger) ; â â â function GetElapsedTicks: TLargeInteger;    function GetElapsedMilliseconds: TLargeInteger;    function GetElapsed: string;â â public  â â â constructor Create(const startOnCreate : boolean bogus) ; â â â procedure Start;â â â â procedure Stop;â â â â property IsHighResolution : boolean read fIsHighResolution;    property ElapsedTicks : TLargeInteger read GetElapsedTicks;    property ElapsedMilliseconds : TLargeInteger read GetElapsedMilliseconds;    property Elapsed : string read GetElapsed;    property IsRunning : boolean read fIsRunning;  end;implementation constructor TStopWat ch.Create(const startOnCreate : boolean bogus) ;start  â inherited Create;   fIsRunning : bogus;   fIsHighResolution : QueryPerformanceFrequency(fFrequency) ;  â if NOT fIsHighResolution then fFrequency : MSecsPerSec;  if startOnCreate then Start;end;function TStopWatch.GetElapsedTicks: TLargeInteger;begin  â result : fStopCount - fStartCount; end;procedure TStopWatch.SetTickStamp(var lInt : TLargeInteger) ;start  â if fIsHighResolution then     QueryPerformanceCounter(lInt)  â else     lInt : MilliSecondOf(Now) ; end;function TStopWatch.GetElapsed: string;var  â dt : TDateTime; start  â dt : ElapsedMilliseconds/MSecsPerSec/SecsPerDay;  â result : Format(%d days, %s, [trunc(dt), FormatDateTime(hh:nn:ss.z, Frac(dt))]) ; end;function TStopWatch.GetElapsedMilliseconds: TLargeInteger;begin  â result : (MSecsPerSec * (fStopCount - fStartCount)) div fFrequency; end;procedure TStopWatch.Start;begin   SetTickStamp(fStartCount) ;   fIsRunning : valid; end;procedure TStopWatch.Stop;begin   SetTickStamp(fStopCount) ;   fIsRunning : bogus; end;end. Heres a case of use: var  â sw : TStopWatch;   elapsedMilliseconds : cardinal; start  â sw : TStopWatch.Create() ;  â try     sw.Start;  â â â //TimeOutThisFunction()     sw.Stop;     elapsedMilliseconds : sw.ElapsedMilliseconds;  â finally     sw.Free;  â end;end;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.