Changeset 315 for trunk/Packages/Common/StopWatch.pas
- Timestamp:
- Jun 19, 2024, 11:15:44 PM (5 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/StopWatch.pas
r43 r315 5 5 6 6 uses 7 {$IFDEF W indows}Windows,{$ENDIF}7 {$IFDEF WINDOWS}Windows,{$ENDIF} 8 8 SysUtils, DateUtils; 9 9 … … 13 13 TStopWatch = class 14 14 private 15 fFrequency: TLargeInteger;16 fIsRunning: Boolean;17 fIsHighResolution: Boolean;18 fStartCount, fStopCount: TLargeInteger;19 procedure SetTickStamp(var lInt : TLargeInteger);15 FFrequency: TLargeInteger; 16 FIsRunning: Boolean; 17 FIsHighResolution: Boolean; 18 FStartCount, fStopCount: TLargeInteger; 19 procedure SetTickStamp(var Value: TLargeInteger); 20 20 function GetElapsedTicks: TLargeInteger; 21 21 function GetElapsedMiliseconds: TLargeInteger; 22 22 function GetElapsed: string; 23 23 public 24 constructor Create(const startOnCreate: Boolean = False) ;24 constructor Create(const StartOnCreate: Boolean = False) ; 25 25 procedure Start; 26 26 procedure Stop; 27 property IsHighResolution : Boolean read fIsHighResolution;28 property ElapsedTicks 29 property ElapsedMiliseconds 30 property Elapsed 31 property IsRunning : Boolean read fIsRunning;27 property IsHighResolution: Boolean read FIsHighResolution; 28 property ElapsedTicks: TLargeInteger read GetElapsedTicks; 29 property ElapsedMiliseconds: TLargeInteger read GetElapsedMiliseconds; 30 property Elapsed: string read GetElapsed; 31 property IsRunning: Boolean read FIsRunning; 32 32 end; 33 33 34 34 35 implementation 35 36 36 constructor TStopWatch.Create(const startOnCreate : boolean = false);37 constructor TStopWatch.Create(const StartOnCreate: Boolean = False); 37 38 begin 38 inherited Create;39 FIsRunning := False; 39 40 40 fIsRunning := False; 41 42 {$IFDEF Windows} 41 {$IFDEF WINDOWS} 43 42 fIsHighResolution := QueryPerformanceFrequency(fFrequency) ; 44 43 {$ELSE} 45 fIsHighResolution := False;44 FIsHighResolution := False; 46 45 {$ENDIF} 47 if NOT fIsHighResolution then fFrequency := MSecsPerSec;46 if NOT FIsHighResolution then FFrequency := MSecsPerSec; 48 47 49 48 if StartOnCreate then Start; … … 52 51 function TStopWatch.GetElapsedTicks: TLargeInteger; 53 52 begin 54 Result := fStopCount - fStartCount;53 Result := FStopCount - FStartCount; 55 54 end; 56 55 57 procedure TStopWatch.SetTickStamp(var lInt : TLargeInteger);56 procedure TStopWatch.SetTickStamp(var Value: TLargeInteger); 58 57 begin 59 if fIsHighResolution then58 if FIsHighResolution then 60 59 {$IFDEF Windows} 61 QueryPerformanceCounter( lInt)60 QueryPerformanceCounter(Value) 62 61 {$ELSE} 63 62 {$ENDIF} 64 63 else 65 lInt := MilliSecondOf(Now);64 Value := MilliSecondOf(Now); 66 65 end; 67 66 68 67 function TStopWatch.GetElapsed: string; 69 68 var 70 dt: TDateTime;69 Elapsed: TDateTime; 71 70 begin 72 dt:= ElapsedMiliseconds / MSecsPerSec / SecsPerDay;73 result := Format('%d days, %s', [Trunc(dt), FormatDateTime('hh:nn:ss.z', Frac(dt))]) ;71 Elapsed := ElapsedMiliseconds / MSecsPerSec / SecsPerDay; 72 Result := Format('%d days, %s', [Trunc(Elapsed), FormatDateTime('hh:nn:ss.z', Frac(Elapsed))]) ; 74 73 end; 75 74 76 75 function TStopWatch.GetElapsedMiliseconds: TLargeInteger; 77 76 begin 78 Result := (MSecsPerSec * (fStopCount - fStartCount)) div fFrequency;77 Result := (MSecsPerSec * (fStopCount - FStartCount)) div FFrequency; 79 78 end; 80 79 81 80 procedure TStopWatch.Start; 82 81 begin 83 SetTickStamp( fStartCount);84 fIsRunning := True;82 SetTickStamp(FStartCount); 83 FIsRunning := True; 85 84 end; 86 85 87 86 procedure TStopWatch.Stop; 88 87 begin 89 SetTickStamp( fStopCount);90 fIsRunning := False;88 SetTickStamp(FStopCount); 89 FIsRunning := False; 91 90 end; 92 91
Note:
See TracChangeset
for help on using the changeset viewer.