[go: up one dir, main page]

Menu

[r37]: / src / common / TicToc.pas  Maximize  Restore  History

Download this file

229 lines (184 with data), 4.2 kB

  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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
unit TicToc;
interface
{ $DEFINE NOMULTIMEDIA}
uses Windows, Dialogs, SysUtils, Math;
const
MAXCHANNEL = 40;
EventsSecond: cardinal = 1000;
type
TMyEvent = record
CurSecond: cardinal;
Counter: cardinal;
LastCounter: cardinal;
end;
TPerformanceCNT = class
Last, Delta, Max, Min, Sum, Stat: int64;
procedure Reset;
procedure Tic;
procedure Toc;
constructor Create;
function PerformanceStr: string;
end;
var
Timer: int64;
Timer2: array[0..MAXCHANNEL] of cardinal;
Events: array[0..MAXCHANNEL] of TMyEvent;
QFrequency: int64;
UseQ: boolean;
{$IFDEF NOMULTIMEDIA}
procedure QueryPerformanceCounter(var q: int64);
function QueryPerformanceFrequency(var q: int64): boolean;
{$ENDIF}
procedure Tic;
procedure Toc;
procedure Tic2(Channel: byte = 1);
function Toc2(Channel: byte = 1): string;
procedure ReticEvent(Channel: byte = 1);
procedure TicEvent(Channel: byte = 1);
function TocEvent(Channel: byte = 1): string;
function MyGetTickCount: cardinal;
function MyGetTime: real;
implementation
{$IFDEF NOMULTIMEDIA}
procedure QueryPerformanceCounter(var q: int64);
begin
q := GetTickCount;
end;
function QueryPerformanceFrequency(var q: int64): boolean;
begin
q := 1000;
Result := True;
end;
{$ENDIF}
function MyGetTickCount: cardinal;
var
q: int64;
begin
if UseQ then
begin
QueryPerformanceCounter(q);
Result := round(q / QFrequency * 1000);
end
else
Result := GetTickCount;
end;
procedure Tic;
begin
QueryPerformanceCounter(Timer);
end;
procedure Toc;
var
q: int64;
begin
QueryPerformanceCounter(q);
ShowMessage('Çàäåðæêà=' + FloatToStr(SimpleRoundTo(
(q - Timer) / QFrequency * 1000)) + ' ìñ');
Timer := q;
end;
procedure Tic2(Channel: byte);
begin
Timer2[Channel] := MyGetTickCount;
end;
function Toc2(Channel: byte): string;
var
i: cardinal;
begin
i := myGetTickCount;
Result := IntToStr(i - Timer2[Channel]) + ' ms';
Timer2[Channel] := i;
end;
procedure ReticEvent(Channel: byte);
begin
with Events[Channel] do
begin
CurSecond := 0;
Counter := 0;
LastCounter := 0;
end;
end;
procedure TicEvent(Channel: byte);
var
c: cardinal;
begin
c := myGetTickCount div EventsSecond;
with Events[Channel] do
if c = CurSecond then
Inc(Counter)
else
begin
LastCounter := Counter;
CurSecond := c;
Counter := 0;
end;
end;
function TocEvent(Channel: byte): string;
var
c: cardinal;
begin
Result := IntToStr(Events[Channel].LastCounter);
c := myGetTickCount div EventsSecond;
with Events[Channel] do
if c <> CurSecond then
begin
LastCounter := Counter;
CurSecond := c;
Counter := 0;
end;
end;
{ TPerformanceCNT }
constructor TPerformanceCNT.Create;
begin
inherited;
Reset;
end;
function TPerformanceCNT.PerformanceStr: string;
begin
if Stat > 0 then
{ Result := 'Delta= ' + IntToStr(Delta) + ' ìêñ' + #13#10 +
'Min= ' + IntToStr(Min) + ' ìêñ' + #13#10 +
'Max= ' + IntToStr(Max) + ' ìêñ' + #13#10 + 'Count= ' + IntToStr(Stat) + #13#10 +
'AVG= ' + IntToStr(round(Sum / Stat)) + ' ìêñ'}
Result := Format('%d(%d) ms', [Delta div 1000, Max div 1000])
else
Result := 'NO DATA';
end;
procedure TPerformanceCNT.Reset;
begin
Stat := 0;
Sum := 0;
Max := 0;
Min := 10000000;
end;
procedure TPerformanceCNT.Tic;
begin
QueryPerformanceCounter(Last);
end;
procedure TPerformanceCNT.Toc;
var
q: int64;
begin
QueryPerformanceCounter(q);
Delta := round((q - Last) / QFrequency * 1000000);
if Delta > Max then
Max := Delta;
if Delta < Min then
Min := Delta;
Stat := Stat + 1;
Sum := Sum + Delta;
Last := q;
end;
function MyGetTime: real;
var
q: int64;
begin
if UseQ then
begin
QueryPerformanceCounter(q);
Result := q / QFrequency;
end
else
Result := GetTickCount / 1000;
end;
begin
UseQ := (QueryPerformanceFrequency(QFrequency)) and (QFrequency > 100);
end.