1 | // Taken from http://delphi.about.com/od/windowsshellapi/a/delphi-high-performance-timer-tstopwatch.htm
|
---|
2 | unit StopWatch;
|
---|
3 |
|
---|
4 | interface
|
---|
5 |
|
---|
6 | uses
|
---|
7 | {$IFDEF Windows}Windows,{$ENDIF}
|
---|
8 | SysUtils, DateUtils;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TLargeInteger = Int64;
|
---|
12 |
|
---|
13 | TStopWatch = class
|
---|
14 | private
|
---|
15 | fFrequency : TLargeInteger;
|
---|
16 | fIsRunning: Boolean;
|
---|
17 | fIsHighResolution: Boolean;
|
---|
18 | fStartCount, fStopCount : TLargeInteger;
|
---|
19 | procedure SetTickStamp(var lInt : TLargeInteger) ;
|
---|
20 | function GetElapsedTicks: TLargeInteger;
|
---|
21 | function GetElapsedMiliseconds: TLargeInteger;
|
---|
22 | function GetElapsed: string;
|
---|
23 | public
|
---|
24 | constructor Create(const startOnCreate : Boolean = False) ;
|
---|
25 | procedure Start;
|
---|
26 | procedure Stop;
|
---|
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 | end;
|
---|
33 |
|
---|
34 | implementation
|
---|
35 |
|
---|
36 | constructor TStopWatch.Create(const startOnCreate : boolean = false) ;
|
---|
37 | begin
|
---|
38 | inherited Create;
|
---|
39 |
|
---|
40 | fIsRunning := False;
|
---|
41 |
|
---|
42 | {$IFDEF Windows}
|
---|
43 | fIsHighResolution := QueryPerformanceFrequency(fFrequency) ;
|
---|
44 | {$ELSE}
|
---|
45 | fIsHighResolution := False;
|
---|
46 | {$ENDIF}
|
---|
47 | if NOT fIsHighResolution then fFrequency := MSecsPerSec;
|
---|
48 |
|
---|
49 | if StartOnCreate then Start;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | function TStopWatch.GetElapsedTicks: TLargeInteger;
|
---|
53 | begin
|
---|
54 | Result := fStopCount - fStartCount;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | procedure TStopWatch.SetTickStamp(var lInt : TLargeInteger) ;
|
---|
58 | begin
|
---|
59 | if fIsHighResolution then
|
---|
60 | {$IFDEF Windows}
|
---|
61 | QueryPerformanceCounter(lInt)
|
---|
62 | {$ELSE}
|
---|
63 | {$ENDIF}
|
---|
64 | else
|
---|
65 | lInt := MilliSecondOf(Now) ;
|
---|
66 | end;
|
---|
67 |
|
---|
68 | function TStopWatch.GetElapsed: string;
|
---|
69 | var
|
---|
70 | dt: TDateTime;
|
---|
71 | begin
|
---|
72 | dt := ElapsedMiliseconds / MSecsPerSec / SecsPerDay;
|
---|
73 | result := Format('%d days, %s', [Trunc(dt), FormatDateTime('hh:nn:ss.z', Frac(dt))]) ;
|
---|
74 | end;
|
---|
75 |
|
---|
76 | function TStopWatch.GetElapsedMiliseconds: TLargeInteger;
|
---|
77 | begin
|
---|
78 | Result := (MSecsPerSec * (fStopCount - fStartCount)) div fFrequency;
|
---|
79 | end;
|
---|
80 |
|
---|
81 | procedure TStopWatch.Start;
|
---|
82 | begin
|
---|
83 | SetTickStamp(fStartCount);
|
---|
84 | fIsRunning := True;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | procedure TStopWatch.Stop;
|
---|
88 | begin
|
---|
89 | SetTickStamp(fStopCount);
|
---|
90 | fIsRunning := False;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | end.
|
---|