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 Value: 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 |
|
---|
35 | implementation
|
---|
36 |
|
---|
37 | constructor TStopWatch.Create(const StartOnCreate: Boolean = False);
|
---|
38 | begin
|
---|
39 | FIsRunning := False;
|
---|
40 |
|
---|
41 | {$IFDEF WINDOWS}
|
---|
42 | fIsHighResolution := QueryPerformanceFrequency(fFrequency) ;
|
---|
43 | {$ELSE}
|
---|
44 | FIsHighResolution := False;
|
---|
45 | {$ENDIF}
|
---|
46 | if NOT FIsHighResolution then FFrequency := MSecsPerSec;
|
---|
47 |
|
---|
48 | if StartOnCreate then Start;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | function TStopWatch.GetElapsedTicks: TLargeInteger;
|
---|
52 | begin
|
---|
53 | Result := FStopCount - FStartCount;
|
---|
54 | end;
|
---|
55 |
|
---|
56 | procedure TStopWatch.SetTickStamp(var Value: TLargeInteger);
|
---|
57 | begin
|
---|
58 | if FIsHighResolution then
|
---|
59 | {$IFDEF Windows}
|
---|
60 | QueryPerformanceCounter(Value)
|
---|
61 | {$ELSE}
|
---|
62 | {$ENDIF}
|
---|
63 | else
|
---|
64 | Value := MilliSecondOf(Now);
|
---|
65 | end;
|
---|
66 |
|
---|
67 | function TStopWatch.GetElapsed: string;
|
---|
68 | var
|
---|
69 | Elapsed: TDateTime;
|
---|
70 | begin
|
---|
71 | Elapsed := ElapsedMiliseconds / MSecsPerSec / SecsPerDay;
|
---|
72 | Result := Format('%d days, %s', [Trunc(Elapsed), FormatDateTime('hh:nn:ss.z', Frac(Elapsed))]) ;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | function TStopWatch.GetElapsedMiliseconds: TLargeInteger;
|
---|
76 | begin
|
---|
77 | Result := (MSecsPerSec * (fStopCount - FStartCount)) div FFrequency;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | procedure TStopWatch.Start;
|
---|
81 | begin
|
---|
82 | SetTickStamp(FStartCount);
|
---|
83 | FIsRunning := True;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TStopWatch.Stop;
|
---|
87 | begin
|
---|
88 | SetTickStamp(FStopCount);
|
---|
89 | FIsRunning := False;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | end.
|
---|