Thursday, August 27, 2020

High Performance Timer in Delphi - TStopWatch

Superior Timer in Delphi - TStopWatch For routine work area database applications, adding a solitary second to an errands execution time once in a while has any kind of effect to end clients - yet when you have to process a great many tree leaves or create billions of novel arbitrary numbers, speed-of-execution turns out to be increasingly significant. Timing Out Your Code In certain applications, extremely exact, high-accuracy time estimation strategies are significant and fortunately Delphi gives a superior counter to qualifyâ these times. Utilizing RTLs Now Function One alternative 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, slipped by : 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 spans the exactness of Now is once in a while insufficient. Utilizing Windows API GetTickCount For significantly increasingly exact information, utilize the GetTickCount Windows API work. GetTickCount recovers the quantity of milliseconds that have passed since the framework was begun, however the capacity just has the exactness of 1 ms and may not generally be precise if the PC stays controlled up for significant stretches of time. The slipped by time is put away as a DWORD (32-piece) esteem. In this way, the time will fold over to zero if Windows is run consistently 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 precision of the framework clock (10/55 ms). High Precision Timing Out Your Code In the event that your PC underpins a high-goals execution counter, utilize the QueryPerformanceFrequency Windows API capacity to communicate the recurrence, in checks 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 an area of code, an application utilizes the counter as a high-goals clock. The exactness of high-goals clocks is around two or three 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 slipped by time by including clock ticks in the fundamental clock system. The IsHighResolution property demonstrates 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 passed time in milliseconds.The Elapsed property gets the all out slipped by 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.