-
Notifications
You must be signed in to change notification settings - Fork 0
/
uStopWatch.pas
178 lines (157 loc) · 3.8 KB
/
uStopWatch.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
unit uStopWatch;
interface
uses
Windows, SysUtils, AcedConsts, uGlobalVars, uGlobalConstants, uGlobalFunctions;
const
StopWatchElapsedDefaultFormat = '%0:s %1:d ms';
type
EStopWatchError = class(Exception);
TSWTimeListRec = record
FT: Int64;
FD: string;
end;
TSWTimeListArray = array of TSWTimeListRec;
TStopWatch = class(TObject)
private
FFR: Int64;
FT0: Int64;
FTList: TSWTimeListArray;
FCount: Integer;
FSize: Integer;
FCurThread: THandle;
FRunning: Boolean;
procedure _QueryPerformanceCounter(var AOut: Int64); inline;
function GetSum: Cardinal;
public
constructor Create;
//---
procedure Clear;
procedure SetCap(ACount: Integer);
//---
procedure Start(const ADesc: string = '');
procedure StartNew(const ADesc: string = '');
procedure Stop;
//---
function StopWatch: Integer;
function StopWatchStr: string;
function Elapsed(const AFormat: string = StopWatchElapsedDefaultFormat;
const ADelimiter: string = CRLF; AShowSum: Boolean = False): string;
//---
property IsRunning: Boolean read FRunning;
property Sum: Cardinal read GetSum;
end;
implementation
{ TStopWatch }
procedure TStopWatch.Clear;
begin
FT0 := 0;
FCount := 0;
FRunning := False;
end;
constructor TStopWatch.Create;
begin
Clear;
SetCap(1);
FCurThread := GetCurrentThread();
QueryPerformanceFrequency(FFR);
end;
procedure TStopWatch.SetCap(ACount: Integer);
begin
if Length(FTList)<ACount then
begin
FSize := G_NormalizeCapacity(ACount);
SetLength(FTList, FSize);
end;
end;
procedure TStopWatch._QueryPerformanceCounter(var AOut: Int64);
var OldMask: DWORD;
begin
oldMask := SetThreadAffinityMask(FCurThread, 1);
QueryPerformanceCounter(AOut);
SetThreadAffinityMask(FCurThread, OldMask);
end;
procedure TStopWatch.Start(const ADesc: string);
var i: Int64;
begin
if FCount>=FSize then
raise EStopWatchError.Create('Buffer full. Use .SetCap()');
_QueryPerformanceCounter(i);
if FRunning then
begin
FTList[FCount-1].FT := i - FT0;
end;
FTList[FCount].FD := ADesc;
FT0 := i;
Inc(FCount);
FRunning := True;
end;
procedure TStopWatch.StartNew(const ADesc: string);
begin
Clear;
Start(ADesc);
end;
procedure TStopWatch.Stop;
var i: Int64;
begin
if FRunning then
begin
_QueryPerformanceCounter(i);
FTList[FCount-1].FT := i - FT0;
end
else
begin
raise EStopWatchError.Create('StopWatch isn''t started');
end;
FRunning := False;
end;
function TStopWatch.StopWatch: Integer;
begin
if FCount=0 then
raise EStopWatchError.Create('First run Start()');
if FRunning then
Stop;
Result := Round(FTList[FCount-1].FT/FFR*1000);
end;
function TStopWatch.StopWatchStr: string;
begin
Result := Format('%d ms', [StopWatch()])
end;
function TStopWatch.Elapsed(const AFormat, ADelimiter: string;
AShowSum: Boolean): string;
var
j: Integer;
t: Cardinal;
k: Cardinal;
f: string;
begin
if FRunning then
Stop;
Result := '';
k := 0;
f := IfElse(AFormat='', StopWatchElapsedDefaultFormat, AFormat);
for j:=0 to FCount-1 do
begin
t := Round(FTList[j].FT / FFR * 1000);
Result := StrAppendWDelim(
Result,
Format(f, [FTList[j].FD, t]),
ADelimiter
);
if AShowSum then
Inc(k, t);
end;
if AShowSum then
Result := StrAppendWDelim(
Result,
Format(f, ['SUM', k]),
ADelimiter
);
end;
function TStopWatch.GetSum: Cardinal;
var j: Integer;
begin
Result := 0;
for j:=0 to FCount-1 do
Inc(Result, Round(FTList[j].FT / FFR * 1000));
end;
end.