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
|
unit frm_main;
{$mode objfpc}{$H+}
// You need to enable these in tiOPF's tiDefines.inc as well.
{.$Define object_tracking}
{$Define reference_counting}
interface
uses
SysUtils, Classes, fpg_base, fpg_main, fpg_edit,
fpg_widget, fpg_form, fpg_label, fpg_button,
fpg_listbox, fpg_memo, fpg_combobox, fpg_grid,
fpg_dialogs, fpg_checkbox, fpg_tree, fpg_trackbar,
fpg_progressbar, fpg_radiobutton, fpg_tab, fpg_menu,
fpg_panel, fpg_popupcalendar, fpg_gauge;
type
TMainForm = class(TfpgForm)
private
procedure PerformanceTestNoReferenceCounting(Sender: TObject);
procedure PerformanceTestReferenceCounting(Sender: TObject);
procedure btnTestValidClick(Sender: TObject);
procedure Log(const AMessage: string);
public
{@VFD_HEAD_BEGIN: MainForm}
btnNoRefCount: TfpgButton;
btnRefCount: TfpgButton;
btnTestValid: TfpgButton;
memName1: TfpgMemo;
lblName1: TfpgLabel;
lblName2: TfpgLabel;
memLog: TfpgMemo;
{@VFD_HEAD_END: MainForm}
constructor Create(AOwner: TComponent); override;
procedure AfterCreate; override;
end;
{@VFD_NEWFORM_DECL}
implementation
uses
tiUtils,
tiBaseObject;
const
CTestRunTime = 5; // Seconds
CTestCount = 1000000; // 1 million
{@VFD_NEWFORM_IMPL}
procedure TMainForm.PerformanceTestNoReferenceCounting(Sender: TObject);
var
LO: TtiBaseObject;
LStart: Cardinal;
LCount: Cardinal;
begin
LCount := 0;
LStart := tiGetTickCount;
while LCount < CTestCount do
begin
LO := TtiBaseObject.Create;
LO.Free;
Inc(LCount);
end;
Log(Format('%s iterations in %d ms (no reference counting)',
[tiIntToCommaStr(LCount), tiGetTickCount - LStart]));
end;
procedure TMainForm.PerformanceTestReferenceCounting(Sender: TObject);
{$ifdef reference_counting}
var
// LO: IInterface;
LO: TtiBaseObject; // We are testing object creation only. Hence a class and not interface reference.
LStart: Cardinal;
LCount: Cardinal;
begin
LCount := 0;
LStart := tiGetTickCount;
while LCount < CTestCount do
begin
LO := TtiBaseObject.CreateWithRefCounting;
LO.Free; // we are testing object creation. This remove the garbage collector.
Inc(LCount);
end;
Log(Format('%s iterations in %d ms (reference counting)',
[tiIntToCommaStr(LCount), tiGetTickCount - LStart]));
{$else}
begin
Assert(False, 'reference_counting not enabled');
{$endif}
end;
procedure TMainForm.btnTestValidClick(Sender: TObject);
var
LO: TtiBaseObject;
begin
LO := TtiBaseObject.Create;
Assert(LO.TestValid);
LO.Free;
Assert(not LO.TestValid);
end;
procedure TMainForm.Log(const AMessage: string);
begin
memLog.Lines.Add(AMessage);
memLog.Invalidate;
end;
constructor TMainForm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
memLog.Lines.Clear;
{$ifdef object_tracking}
Log('object_tracking is ON');
{$else}
btnTestValid.Enabled := False;
Log('object_tracking is OFF');
{$endif}
{$ifdef reference_counting}
Log('reference_counting is ON');
{$else}
Log('reference_counting is OFF');
btnRefCount.Enabled := False;
{$endif}
Log('---');
end;
procedure TMainForm.AfterCreate;
begin
{@VFD_BODY_BEGIN: MainForm}
Name := 'MainForm';
SetPosition(307, 319, 357, 290);
WindowTitle := 'TtiBaseObject Performance Demo';
WindowPosition := wpScreenCenter;
btnNoRefCount := TfpgButton.Create(self);
with btnNoRefCount do
begin
Name := 'btnNoRefCount';
SetPosition(16, 28, 155, 23);
Text := 'No reference counting';
FontDesc := '#Label1';
ImageName := '';
OnClick := @PerformanceTestNoReferenceCounting;
end;
btnRefCount := TfpgButton.Create(self);
with btnRefCount do
begin
Name := 'btnRefCount';
SetPosition(16, 56, 155, 23);
Text := 'Reference counting';
FontDesc := '#Label1';
ImageName := '';
OnClick := @PerformanceTestReferenceCounting;
end;
btnTestValid := TfpgButton.Create(self);
with btnTestValid do
begin
Name := 'btnTestValid';
SetPosition(16, 112, 155, 23);
Text := 'TtiBaseObject.TestValid';
FontDesc := '#Label1';
ImageName := '';
OnClick := @btnTestValidClick;
end;
memName1 := TfpgMemo.Create(self);
with memName1 do
begin
Name := 'memName1';
SetPosition(176, 28, 176, 125);
Lines.Add('Toggle the conditional defines');
Lines.Add('REFERENCE_COUNTING ');
Lines.Add('and OBJECT_TRACKING on ');
Lines.Add('and off to examine behaviour.');
FontDesc := '#Edit1';
Enabled := False;
end;
lblName1 := TfpgLabel.Create(self);
with lblName1 do
begin
Name := 'lblName1';
SetPosition(8, 8, 160, 15);
Text := 'Performance';
FontDesc := '#Label2';
end;
lblName2 := TfpgLabel.Create(self);
with lblName2 do
begin
Name := 'lblName2';
SetPosition(8, 92, 160, 15);
Text := 'TtiBaseObject.TestValid';
FontDesc := '#Label2';
end;
memLog := TfpgMemo.Create(self);
with memLog do
begin
Name := 'memLog';
SetPosition(8, 164, 344, 121);
Lines.Add('');
FontDesc := '#Edit1';
end;
{@VFD_BODY_END: MainForm}
end;
end.
|