source: trunk/Packages/TemplateGenerics/Demo/UMainForm.pas

Last change on this file was 1, checked in by chronos, 8 years ago
  • Imported initial version.
File size: 22.5 KB
Line 
1unit UMainForm;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 ComCtrls, SpecializedList, SpecializedDictionary, SpecializedQueue,
10 DateUtils, SpecializedMatrix, SpecializedStream;
11
12type
13
14 { TMainForm }
15
16 TMainForm = class(TForm)
17 ButtonStreamByte: TButton;
18 ButtonBenchmarkDictionary: TButton;
19 ButtonBenchmarkListPointer: TButton;
20 ButtonListObject: TButton;
21 ButtonBenchmarkListString: TButton;
22 ButtonCharList: TButton;
23 ButtonMatrixInteger: TButton;
24 ButtonQueueInteger: TButton;
25 ButtonDictionaryString: TButton;
26 ButtonIntegerList: TButton;
27 ButtonStringList: TButton;
28 Label1: TLabel;
29 LabelTestName: TLabel;
30 ListViewOutput: TListView;
31 procedure ButtonBenchmarkDictionaryClick(Sender: TObject);
32 procedure ButtonBenchmarkListPointerClick(Sender: TObject);
33 procedure ButtonBenchmarkListStringClick(Sender: TObject);
34 procedure ButtonCharListClick(Sender: TObject);
35 procedure ButtonDictionaryStringClick(Sender: TObject);
36 procedure ButtonIntegerListClick(Sender: TObject);
37 procedure ButtonMatrixIntegerClick(Sender: TObject);
38 procedure ButtonListObjectClick(Sender: TObject);
39 procedure ButtonQueueIntegerClick(Sender: TObject);
40 procedure ButtonStringListClick(Sender: TObject);
41 procedure ButtonStreamByteClick(Sender: TObject);
42 procedure FormCreate(Sender: TObject);
43 procedure FormDestroy(Sender: TObject);
44 public
45 MeasureDuration: TDateTime;
46 Bitmap: TBitmap;
47 procedure UpdateButtonState(Enabled: Boolean);
48 procedure WriteOutput(Text1: string = ''; Text2: string = '');
49 end;
50
51var
52 MainForm: TMainForm;
53
54implementation
55
56{$R *.lfm}
57
58{ TMainForm }
59
60procedure TMainForm.FormCreate(Sender: TObject);
61begin
62 MeasureDuration := 100 * OneMillisecond;
63end;
64
65procedure TMainForm.ButtonIntegerListClick(Sender: TObject);
66var
67 List: TListInteger;
68 List2: TListInteger;
69 I: Integer;
70begin
71 ListViewOutput.Clear;
72 LabelTestName.Caption := 'TListInteger test';
73 List := TListInteger.Create;
74 List2 := TListInteger.Create;
75 with List do try
76 AddArray([10, 20, 30, 40]);
77 WriteOutput('AddArray([10, 20, 30, 40])', Implode(',', IntToStr));
78 Clear;
79 WriteOutput('Clear', Implode(',', IntToStr));
80 for I := 0 to 10 do Add(I);
81 WriteOutput('for I := 0 to 10 do Add(I)', Implode(',', IntToStr));
82 WriteOutput('Count', IntToStr(Count));
83 Reverse;
84 WriteOutput('Reverse', Implode(',', IntToStr));
85 WriteOutput('First', IntToStr(First));
86 WriteOutput('Last', IntToStr(Last));
87 MoveItems(3, 2, 3);
88 WriteOutput('MoveItems(3, 2, 3)', Implode(',', IntToStr));
89 Insert(5, 11);
90 WriteOutput('Insert(5, 11)', Implode(',', IntToStr));
91 DeleteItems(0, 10);
92 WriteOutput('Delete(0, 10)', Implode(',', IntToStr));
93 List2.SetArray([1, 0]);
94 WriteOutput('EqualTo([6, 11])', BoolToStr(EqualTo(List2)));
95 List2.SetArray([2, 0]);
96 WriteOutput('EqualTo([7, 11])', BoolToStr(EqualTo(List2)));
97 InsertCount(0, 3);
98 WriteOutput('InsertCount(0, 3)', Implode(',', IntToStr));
99 Fill(0, 3, 9);
100 WriteOutput('Fill(0, 3, 9)', Implode(',', IntToStr));
101 finally
102 Free;
103 List2.Free;
104 end;
105end;
106
107procedure TMainForm.ButtonMatrixIntegerClick(Sender: TObject);
108var
109 Matrix: TMatrixInteger;
110 I: Integer;
111begin
112 ListViewOutput.Clear;
113 LabelTestName.Caption := 'TMatrixInteger test';
114 Matrix := TMatrixInteger.Create;
115 with Matrix do try
116 Count := CreateIndex(2, 2);
117 WriteOutput('Count := CreateIndex(2, 2)', '[' + Implode('; ', ', ', IntToStr) + ']');
118 Fill(CreateIndex(0, 0), Count, 1);
119 WriteOutput('Fill(1)', '[' + Implode('; ', ', ', IntToStr) + ']');
120 Count := CreateIndex(3, 3);
121 WriteOutput('Count := CreateIndex(3, 3)', '[' + Implode('; ', ', ', IntToStr) + ']');
122 WriteOutput('Count [Y, X]', IntToStr(Count.Y) + ', ' + IntToStr(Count.X));
123 Clear;
124 WriteOutput('Clear', '[' + Implode('; ', ', ', IntToStr) + ']');
125 WriteOutput('Count [Y, X]', IntToStr(Count.Y) + ', ' + IntToStr(Count.X));
126 finally
127 Free;
128 end;
129end;
130
131procedure TMainForm.ButtonStreamByteClick(Sender: TObject);
132var
133 Stream: TMemoryStreamByte;
134 I: Integer;
135 ByteArray: array of Byte;
136 ByteArrayText: string;
137begin
138 ListViewOutput.Clear;
139 LabelTestName.Caption := 'TStreamByte test';
140 Stream := TMemoryStreamByte.Create;
141 with Stream do try
142 WriteOutput('Size := ', IntToStr(Stream.Size));
143 Write(1);
144 WriteOutput('Write(1)', '');
145 WriteOutput('Size, Position', IntToStr(Stream.Size) + ', ' + IntToStr(Stream.Position));
146 WriteArray([2, 3, 4]);
147 WriteOutput('WriteArray([2, 3, 4])', '');
148 WriteOutput('Size, Position', IntToStr(Stream.Size) + ', ' + IntToStr(Stream.Position));
149 Position := 1;
150 WriteOutput('Position := 1', '');
151 WriteOutput('Size, Position', IntToStr(Stream.Size) + ', ' + IntToStr(Stream.Position));
152 WriteOutput('Read', IntToStr(Read));
153 WriteOutput('Size, Position', IntToStr(Stream.Size) + ', ' + IntToStr(Stream.Position));
154 ByteArray := ReadArray(2);
155 ByteArrayText := '[';
156 for I := 0 to Length(ByteArray) - 1 do begin
157 ByteArrayText := ByteArrayText + IntToStr(ByteArray[I]);
158 if I < Length(ByteArray) - 1 then ByteArrayText := ByteArrayText + ', ';
159 end;
160 ByteArrayText := ByteArrayText + ']';
161 WriteOutput('ReadArray', ByteArrayText);
162 WriteOutput('Size, Position', IntToStr(Stream.Size) + ', ' + IntToStr(Stream.Position));
163 finally
164 Free;
165 end;
166end;
167
168function ObjectToStr(Obj: TObject): string;
169begin
170 Result := Obj.ClassName;
171end;
172
173procedure TMainForm.ButtonListObjectClick(Sender: TObject);
174var
175 List: TListObject;
176 I: Integer;
177begin
178 ListViewOutput.Clear;
179 LabelTestName.Caption := 'TListObject test';
180 List := TListObject.Create;
181 with List do try
182 AddArray([TObject.Create, TObject.Create, TObject.Create, TObject.Create]);
183 WriteOutput('AddArray([TObject.Create, TObject.Create, TObject.Create, TObject.Create])', Implode(',', ObjectToStr));
184 Clear;
185 WriteOutput('Clear', Implode(',', ObjectToStr));
186 for I := 0 to 10 do Add(TObject.Create);
187 WriteOutput('for I := 0 to 10 do Add(TObject.Create)', Implode(',', ObjectToStr));
188 WriteOutput('Count', IntToStr(Count));
189 Reverse;
190 WriteOutput('Reverse', Implode(',', ObjectToStr));
191 MoveItems(3, 2, 3);
192 WriteOutput('MoveItems(3, 2, 3)', Implode(',', ObjectToStr));
193 finally
194 Free;
195 end;
196end;
197
198procedure TMainForm.ButtonQueueIntegerClick(Sender: TObject);
199var
200 Queue: TQueueInteger;
201 I: Integer;
202begin
203 ListViewOutput.Clear;
204 LabelTestName.Caption := 'TQueueInteger test';
205 Queue := TQueueInteger.Create;
206 with Queue do try
207 Enqueue(1);
208 Enqueue(2);
209 Enqueue(3);
210 WriteOutput('Enqueue(1),Enqueue(2),Enqueue(3) ', List.Implode(',', IntToStr));
211 Enqueue(4);
212 WriteOutput('Enqueue(4)', List.Implode(',', IntToStr));
213 WriteOutput('Dequeued item', IntToStr(Dequeue));
214 WriteOutput('Dequeue', List.Implode(',', IntToStr));
215 finally
216 Free;
217 end;
218end;
219
220function StringPairToStr(Pair: TPairStringString): string;
221begin
222 Result := Pair.Key + ':' + Pair.Value;
223end;
224
225procedure TMainForm.ButtonDictionaryStringClick(Sender: TObject);
226var
227 Dictionary: TDictionaryStringString;
228begin
229 ListViewOutput.Clear;
230 LabelTestName.Caption := 'TDictionaryString test';
231 Dictionary := TDictionaryStringString.Create;
232 with Dictionary do try
233 Add('Key1', 'Value1');
234 Add('Key2', 'Value2');
235 Add('Key3', 'Value3');
236 WriteOutput('Add(''Key1'', ''Value1''),Add(''Key1'', ''Value1''),Add(''Key1'', ''Value1'')', Implode(',', StringPairToStr));
237 WriteOutput('Values[Key2]', Values['Key2']);
238 WriteOutput('Values[Key2] = None');
239 Values['Key2'] := 'None';
240 WriteOutput('Values[Key2]', Values['Key2']);
241 WriteOutput('Values[Key0]', Values['Key0']);
242 WriteOutput('Keys[2]', Keys[2]);
243 finally
244 Free;
245 end;
246end;
247
248function CharToStr(Value: Char): string;
249begin
250 Result := Value;
251end;
252
253procedure TMainForm.ButtonCharListClick(Sender: TObject);
254var
255 List: TListChar;
256 List2: TListChar;
257begin
258 ListViewOutput.Clear;
259 LabelTestName.Caption := 'TListChar test';
260 List := TListChar.Create;
261 List2 := TListChar.Create;
262 with List do try
263 AddArray([' ', ' ', 'A', 'b', 'c', 'd', ' ']);
264 WriteOutput('AddArray(['' '', '' '', ''A'', ''b'', ''c'', ''d'', '' ''])',
265 '''' + Implode('', CharToStr) + '''');
266 Reverse;
267 WriteOutput('Reverse', '''' + Implode('', CharToStr) + '''');
268 TrimLeft;
269 WriteOutput('TrimLeft', '''' + Implode('', CharToStr) + '''');
270 TrimRight;
271 WriteOutput('TrimRight', '''' + Implode('', CharToStr) + '''');
272 UpperCase;
273 WriteOutput('UpperCase', '''' + Implode('', CharToStr) + '''');
274 LowerCase;
275 WriteOutput('LowerCase', '''' + Implode('', CharToStr) + '''');
276 WriteOutput('IndexOf(''c'')', IntToStr(IndexOf('c')));
277 List2.AddArray(['c', 'b']);
278 WriteOutput('IndexOfList(''cb'')', IntToStr(IndexOfList(List2)));
279 finally
280 List2.Free;
281 Free;
282 end;
283end;
284
285procedure TMainForm.ButtonBenchmarkListStringClick(Sender: TObject);
286var
287 List: TListString;
288 List2: TStringList;
289 StartTime: TDateTime;
290 I: Integer;
291const
292 SampleText: string = 'text';
293 SampleCount: Integer = 100000;
294begin
295 LabelTestName.Caption := 'Generic specialized TListString vs. classic non-generic TStringList benchmark';
296 ListViewOutput.Clear;
297 try
298 UpdateButtonState(False);
299 List := TListString.Create;
300 List2 := TStringList.Create;
301
302 StartTime := Now;
303 repeat
304 List.Add(SampleText);
305 until (Now - StartTime) > MeasureDuration;
306 WriteOutput('TListString.Add', IntToStr(List.Count) + ' ops');
307 List.Clear;
308 Application.ProcessMessages;
309
310 StartTime := Now;
311 repeat
312 List2.Add(SampleText);
313 until (Now - StartTime) > MeasureDuration;
314 WriteOutput('TStringList.Add', IntToStr(List2.Count) + ' ops');
315 List2.Clear;
316 Application.ProcessMessages;
317
318 StartTime := Now;
319 repeat
320 List.Insert(0, SampleText);
321 until (Now - StartTime) > MeasureDuration;
322 WriteOutput('TListString.Insert', IntToStr(List.Count) + ' ops');
323 List.Clear;
324 Application.ProcessMessages;
325
326 StartTime := Now;
327 repeat
328 List2.Insert(0, SampleText);
329 until (Now - StartTime) > MeasureDuration;
330 WriteOutput('TStringList.Insert', IntToStr(List2.Count) + ' ops');
331 List2.Clear;
332 Application.ProcessMessages;
333
334 for I := 0 to SampleCount - 1 do
335 List.Add(SampleText);
336 StartTime := Now;
337 I := 0;
338 repeat
339 List.Delete(0);
340 Inc(I);
341 until (Now - StartTime) > MeasureDuration;
342 WriteOutput('TListString.Delete', IntToStr(I) + ' ops');
343 List.Clear;
344 Application.ProcessMessages;
345
346 for I := 0 to SampleCount - 1 do
347 List2.Add(SampleText);
348 StartTime := Now;
349 I := 0;
350 repeat
351 List2.Delete(0);
352 Inc(I);
353 until (Now - StartTime) > MeasureDuration;
354 WriteOutput('TStringList.Delete', IntToStr(I) + ' ops');
355 Application.ProcessMessages;
356
357 for I := 0 to SampleCount - 1 do
358 List.Add(SampleText);
359 StartTime := Now;
360 I := 0;
361 repeat
362 List.Move(Round(SampleCount * 0.3), Round(SampleCount * 0.7));
363 Inc(I);
364 until (Now - StartTime) > MeasureDuration;
365 WriteOutput('TListString.Move', IntToStr(I) + ' ops');
366 List.Clear;
367 Application.ProcessMessages;
368
369 for I := 0 to SampleCount - 1 do
370 List2.Add(SampleText);
371 StartTime := Now;
372 I := 0;
373 repeat
374 List2.Move(Round(SampleCount * 0.3), Round(SampleCount * 0.7));
375 Inc(I);
376 until (Now - StartTime) > MeasureDuration;
377 WriteOutput('TStringList.Move', IntToStr(I) + ' ops');
378 Application.ProcessMessages;
379
380 for I := 0 to SampleCount - 1 do
381 List.Add(SampleText);
382 StartTime := Now;
383 I := 0;
384 repeat
385 List.Exchange(Round(SampleCount * 0.3), Round(SampleCount * 0.7));
386 Inc(I);
387 until (Now - StartTime) > MeasureDuration;
388 WriteOutput('TListString.Exchange', IntToStr(I) + ' ops');
389 List.Clear;
390 Application.ProcessMessages;
391
392 for I := 0 to SampleCount - 1 do
393 List2.Add(SampleText);
394 StartTime := Now;
395 I := 0;
396 repeat
397 List2.Exchange(Round(SampleCount * 0.3), Round(SampleCount * 0.7));
398 Inc(I);
399 until (Now - StartTime) > MeasureDuration;
400 WriteOutput('TStringList.Exchange', IntToStr(I) + ' ops');
401 Application.ProcessMessages;
402
403 for I := 0 to SampleCount - 1 do
404 List.Add(SampleText + IntToStr(I));
405 StartTime := Now;
406 I := 0;
407 repeat
408 List.IndexOf(SampleText + IntToStr(I mod List.Count));
409 Inc(I);
410 until (Now - StartTime) > MeasureDuration;
411 WriteOutput('TListString.IndexOf', IntToStr(I) + ' ops');
412 List.Clear;
413 Application.ProcessMessages;
414
415 for I := 0 to SampleCount - 1 do
416 List2.Add(SampleText + IntToStr(I));
417 StartTime := Now;
418 I := 0;
419 repeat
420 List2.IndexOf(SampleText + IntToStr(I mod List2.Count));
421 Inc(I);
422 until (Now - StartTime) > MeasureDuration;
423 WriteOutput('TStringList.IndexOf', IntToStr(I) + ' ops');
424 Application.ProcessMessages;
425
426 finally
427 UpdateButtonState(True);
428 List.Free;
429 List2.Free;
430 end;
431end;
432
433procedure TMainForm.ButtonBenchmarkDictionaryClick(Sender: TObject);
434var
435 Dictionary: TDictionaryStringString;
436 Dictionary2: TStringList;
437 StartTime: TDateTime;
438 I: Integer;
439 R: string;
440begin
441 LabelTestName.Caption := 'Generic specialized TDictionaryStringString vs. classic non-generic TStringList benchmark';
442 ListViewOutput.Clear;
443 try
444 UpdateButtonState(False);
445 Dictionary := TDictionaryStringString.Create;
446 Dictionary2 := TStringList.Create;
447 Dictionary2.NameValueSeparator := '|';
448
449 I := 0;
450 StartTime := Now;
451 repeat
452 Dictionary.Add(IntToStr(I), IntToStr(I));
453 I := I + 1;
454 until (Now - StartTime) > MeasureDuration;
455 WriteOutput('TDictionaryStringString.Add', IntToStr(Dictionary.Count) + ' ops');
456 Application.ProcessMessages;
457
458 I := 0;
459 StartTime := Now;
460 repeat
461 Dictionary2.Add(IntToStr(I) + Dictionary2.NameValueSeparator + IntToStr(I));
462 I := I + 1;
463 until (Now - StartTime) > MeasureDuration;
464 WriteOutput('TStringList.Add', IntToStr(Dictionary2.Count) + ' ops');
465 Application.ProcessMessages;
466
467 I := 0;
468 StartTime := Now;
469 repeat
470 R := Dictionary.Values[IntToStr(I mod Dictionary.Count)];
471 I := I + 1;
472 until (Now - StartTime) > MeasureDuration;
473 WriteOutput('TDictionaryStringString.Values', IntToStr(I) + ' ops');
474 Application.ProcessMessages;
475
476 I := 0;
477 StartTime := Now;
478 repeat
479 R := Dictionary2.Values[IntToStr(I mod Dictionary2.Count)];
480 I := I + 1;
481 until (Now - StartTime) > MeasureDuration;
482 WriteOutput('TStringList.Values', IntToStr(I) + ' ops');
483 Application.ProcessMessages;
484
485 I := 0;
486 StartTime := Now;
487 repeat
488 R := Dictionary.Keys[I mod Dictionary.Count];
489 I := I + 1;
490 until (Now - StartTime) > MeasureDuration;
491 WriteOutput('TDictionaryStringString.Keys', IntToStr(I) + ' ops');
492 Application.ProcessMessages;
493
494 I := 0;
495 StartTime := Now;
496 repeat
497 R := Dictionary2.Names[I mod Dictionary2.Count];
498 I := I + 1;
499 until (Now - StartTime) > MeasureDuration;
500 WriteOutput('TStringList.Keys(Names)', IntToStr(I) + ' ops');
501 Application.ProcessMessages;
502
503 I := 0;
504 StartTime := Now;
505 repeat
506 R := Dictionary.Items[I mod Dictionary.Count].Value;
507 I := I + 1;
508 until (Now - StartTime) > MeasureDuration;
509 WriteOutput('TDictionaryStringString.Items', IntToStr(I) + ' ops');
510 Application.ProcessMessages;
511
512 I := 0;
513 StartTime := Now;
514 repeat
515 R := Dictionary2.ValueFromIndex[I mod Dictionary2.Count];
516 I := I + 1;
517 until (Now - StartTime) > MeasureDuration;
518 WriteOutput('TStringList.Items(ValueFromIndex)', IntToStr(I) + ' ops');
519 Application.ProcessMessages;
520
521 finally
522 UpdateButtonState(True);
523 Dictionary.Free;
524 Dictionary2.Free;
525 end;
526end;
527
528procedure TMainForm.ButtonBenchmarkListPointerClick(Sender: TObject);
529var
530 List: TListPointer;
531 List2: TFPList;
532 StartTime: TDateTime;
533 I: Integer;
534const
535 SampleCount: Integer = 100000;
536begin
537 LabelTestName.Caption := 'Generic specialized TListObject vs. classic non-generic TFPList benchmark';
538 ListViewOutput.Clear;
539 try
540 UpdateButtonState(False);
541 List := TListPointer.Create;
542 List2 := TFPList.Create;
543
544 WriteOutput('TListPointer.InstanceSize', IntToStr(TListPointer.InstanceSize) + ' bytes');
545 WriteOutput('TFPList.InstanceSize', IntToStr(TFPList.InstanceSize) + ' bytes');
546
547 StartTime := Now;
548 repeat
549 List.Add(Pointer(1));
550 until (Now - StartTime) > MeasureDuration;
551 WriteOutput('TListPointer.Add', IntToStr(List.Count) + ' ops');
552 List.Clear;
553 Application.ProcessMessages;
554
555 StartTime := Now;
556 repeat
557 List2.Add(Pointer(1));
558 until (Now - StartTime) > MeasureDuration;
559 WriteOutput('TFPList.Add', IntToStr(List2.Count) + ' ops');
560 List2.Clear;
561 Application.ProcessMessages;
562
563 StartTime := Now;
564 repeat
565 List.Insert(0, Pointer(1));
566 until (Now - StartTime) > MeasureDuration;
567 WriteOutput('TListPointer.Insert', IntToStr(List.Count) + ' ops');
568 List.Clear;
569 Application.ProcessMessages;
570
571 StartTime := Now;
572 repeat
573 List2.Insert(0, Pointer(1));
574 until (Now - StartTime) > MeasureDuration;
575 WriteOutput('TFPList.Insert', IntToStr(List2.Count) + ' ops');
576 List2.Clear;
577 Application.ProcessMessages;
578
579 for I := 0 to SampleCount - 1 do
580 List.Add(Pointer(1));
581 StartTime := Now;
582 I := 0;
583 repeat
584 List.Delete(0);
585 Inc(I);
586 until (Now - StartTime) > MeasureDuration;
587 WriteOutput('TListPointer.Delete', IntToStr(I) + ' ops');
588 List.Clear;
589 Application.ProcessMessages;
590
591 for I := 0 to SampleCount - 1 do
592 List2.Add(Pointer(1));
593 StartTime := Now;
594 I := 0;
595 repeat
596 List2.Delete(0);
597 Inc(I);
598 until (Now - StartTime) > MeasureDuration;
599 WriteOutput('TFPList.Delete', IntToStr(I) + ' ops');
600 Application.ProcessMessages;
601
602 for I := 0 to SampleCount - 1 do
603 List.Add(Pointer(1));
604 StartTime := Now;
605 I := 0;
606 repeat
607 List.Move(Round(SampleCount * 0.3), Round(SampleCount * 0.7));
608 Inc(I);
609 until (Now - StartTime) > MeasureDuration;
610 WriteOutput('TListPointer.Move', IntToStr(I) + ' ops');
611 List.Clear;
612 Application.ProcessMessages;
613
614 for I := 0 to SampleCount - 1 do
615 List2.Add(Pointer(1));
616 StartTime := Now;
617 I := 0;
618 repeat
619 List2.Move(Round(SampleCount * 0.3), Round(SampleCount * 0.7));
620 Inc(I);
621 until (Now - StartTime) > MeasureDuration;
622 WriteOutput('TFPList.Move', IntToStr(I) + ' ops');
623 Application.ProcessMessages;
624
625 for I := 0 to SampleCount - 1 do
626 List.Add(Pointer(1));
627 StartTime := Now;
628 I := 0;
629 repeat
630 List.Exchange(Round(SampleCount * 0.3), Round(SampleCount * 0.7));
631 Inc(I);
632 until (Now - StartTime) > MeasureDuration;
633 WriteOutput('TListPointer.Exchange', IntToStr(I) + ' ops');
634 List.Clear;
635 Application.ProcessMessages;
636
637 for I := 0 to SampleCount - 1 do
638 List2.Add(Pointer(1));
639 StartTime := Now;
640 I := 0;
641 repeat
642 List2.Exchange(Round(SampleCount * 0.3), Round(SampleCount * 0.7));
643 Inc(I);
644 until (Now - StartTime) > MeasureDuration;
645 WriteOutput('TFPList.Exchange', IntToStr(I) + ' ops');
646 Application.ProcessMessages;
647
648 for I := 0 to SampleCount - 1 do
649 List.Add(Pointer(1));
650 StartTime := Now;
651 I := 0;
652 repeat
653 List.IndexOf(Pointer(I mod List.Count));
654 Inc(I);
655 until (Now - StartTime) > MeasureDuration;
656 WriteOutput('TListPointer.IndexOf', IntToStr(I) + ' ops');
657 List.Clear;
658 Application.ProcessMessages;
659
660 for I := 0 to SampleCount - 1 do
661 List2.Add(Pointer(1));
662 StartTime := Now;
663 I := 0;
664 repeat
665 List2.IndexOf(Pointer(I mod List2.Count));
666 Inc(I);
667 until (Now - StartTime) > MeasureDuration;
668 WriteOutput('TFPList.IndexOf', IntToStr(I) + ' ops');
669 Application.ProcessMessages;
670
671 for I := 0 to SampleCount - 1 do
672 List.Add(Pointer(1));
673 StartTime := Now;
674 I := 0;
675 repeat
676 List[I mod List.Count] := Pointer(1);
677 Inc(I);
678 until (Now - StartTime) > MeasureDuration;
679 WriteOutput('TListPointer[I] write', IntToStr(I) + ' ops');
680 List.Clear;
681 Application.ProcessMessages;
682
683 for I := 0 to SampleCount - 1 do
684 List2.Add(Pointer(1));
685 StartTime := Now;
686 I := 0;
687 repeat
688 List2[I mod List2.Count] := Pointer(1);
689 Inc(I);
690 until (Now - StartTime) > MeasureDuration;
691 WriteOutput('TFPList[I] write', IntToStr(I) + ' ops');
692 Application.ProcessMessages;
693
694 for I := 0 to SampleCount - 1 do
695 List.Add(Pointer(1));
696 StartTime := Now;
697 I := 0;
698 repeat
699 List[I mod List.Count];
700 Inc(I);
701 until (Now - StartTime) > MeasureDuration;
702 WriteOutput('TListPointer[I] read', IntToStr(I) + ' ops');
703 List.Clear;
704 Application.ProcessMessages;
705
706 for I := 0 to SampleCount - 1 do
707 List2.Add(Pointer(1));
708 StartTime := Now;
709 I := 0;
710 repeat
711 List2[I mod List2.Count];
712 Inc(I);
713 until (Now - StartTime) > MeasureDuration;
714 WriteOutput('TFPList[I] read', IntToStr(I) + ' ops');
715 Application.ProcessMessages;
716 finally
717 UpdateButtonState(True);
718 List.Free;
719 List2.Free;
720 end;
721end;
722
723function StrToStr(Value: string): string;
724begin
725 Result := Value;
726end;
727
728procedure TMainForm.ButtonStringListClick(Sender: TObject);
729var
730 List: TListString;
731begin
732 ListViewOutput.Clear;
733 WriteOutput('TListString test');
734 List := TListString.Create;
735 with List do try
736 AddArray(['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven']);
737 WriteOutput('Count', IntToStr(Count));
738 WriteOutput('Implode', Implode(',', StrToStr));
739 WriteOutput('Reverse');
740 Reverse;
741 WriteOutput('Implode', Implode(',', StrToStr));
742 WriteOutput('First', First);
743 WriteOutput('Last', Last);
744 MoveItems(2, 3, 3);
745 WriteOutput('Implode', Implode(',', StrToStr));
746 InsertCount(0, 3);
747 WriteOutput('InsertCount(0, 3)', Implode(',', StrToStr));
748 Fill(0, 3, 'Zero');
749 WriteOutput('Fill(0, 3, ''Zero'')', Implode(',', StrToStr));
750 finally
751 Free;
752 end;
753end;
754
755procedure TMainForm.FormDestroy(Sender: TObject);
756begin
757end;
758
759procedure TMainForm.UpdateButtonState(Enabled: Boolean);
760begin
761 ButtonBenchmarkDictionary.Enabled := Enabled;
762 ButtonBenchmarkListString.Enabled := Enabled;
763 ButtonCharList.Enabled := Enabled;
764 ButtonDictionaryString.Enabled := Enabled;
765 ButtonIntegerList.Enabled := Enabled;
766 ButtonListObject.Enabled := Enabled;
767 ButtonMatrixInteger.Enabled := Enabled;
768 ButtonQueueInteger.Enabled := Enabled;
769 ButtonStringList.Enabled := Enabled;
770end;
771
772procedure TMainForm.WriteOutput(Text1: string = ''; Text2: string = '');
773var
774 NewItem: TListItem;
775begin
776 NewItem := ListViewOutput.Items.Add;
777 NewItem.Caption := Text1;
778 NewItem.SubItems.Add(Text2);
779end;
780
781end.
782
Note: See TracBrowser for help on using the repository browser.