Changeset 108 for Generics/TemplateGenerics/Demo/UMainForm.pas
- Timestamp:
- Jan 1, 2011, 10:30:09 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/TemplateGenerics/Demo/UMainForm.pas
r107 r108 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 9 ComCtrls, SpecializedList, SpecializedDictionary, SpecializedQueue, 10 DateUtils ;10 DateUtils, SpecializedMatrix; 11 11 12 12 type … … 19 19 ButtonBenchmarkList: TButton; 20 20 ButtonCharList: TButton; 21 ButtonMatrixInteger: TButton; 21 22 ButtonQueueInteger: TButton; 22 23 ButtonDictionaryString: TButton; 23 24 ButtonIntegerList: TButton; 24 25 ButtonStringList: TButton; 25 MemoOutput: TMemo;26 ListViewOutput: TListView; 26 27 procedure ButtonBenchmarkDictionaryClick(Sender: TObject); 27 28 procedure ButtonBenchmarkListClick(Sender: TObject); … … 29 30 procedure ButtonDictionaryStringClick(Sender: TObject); 30 31 procedure ButtonIntegerListClick(Sender: TObject); 32 procedure ButtonMatrixIntegerClick(Sender: TObject); 31 33 procedure ButtonListObjectClick(Sender: TObject); 32 34 procedure ButtonQueueIntegerClick(Sender: TObject); … … 37 39 public 38 40 Bitmap: TBitmap; 39 procedure Write Ln(Text: string = '');41 procedure WriteOutput(Text1: string = ''; Text2: string = ''); 40 42 end; 41 43 … … 58 60 I: Integer; 59 61 begin 60 MemoOutput.Clear;61 Write Ln('TListInteger test');62 ListViewOutput.Clear; 63 WriteOutput('TListInteger test'); 62 64 List := TListInteger.Create; 63 65 with List do try 64 WriteLn('AddArray([10, 20, 30, 40])');65 66 AddArray([10, 20, 30, 40]); 66 WriteLn('Implode: ' + Implode(',', IntToStr)); 67 WriteOutput('AddArray([10, 20, 30, 40])', Implode(',', IntToStr)); 68 Clear; 69 WriteOutput('Clear', Implode(',', IntToStr)); 70 for I := 0 to 10 do Add(I); 71 WriteOutput('for I := 0 to 10 do Add(I)', Implode(',', IntToStr)); 72 WriteOutput('Count', IntToStr(Count)); 73 Reverse; 74 WriteOutput('Reverse', Implode(',', IntToStr)); 75 WriteOutput('First', IntToStr(First)); 76 WriteOutput('Last', IntToStr(Last)); 77 MoveItems(3, 2, 3); 78 WriteOutput('MoveItems(3, 2, 3)', Implode(',', IntToStr)); 79 Insert(5, 11); 80 WriteOutput('Insert(5, 11)', Implode(',', IntToStr)); 81 finally 82 Free; 83 end; 84 end; 85 86 procedure TMainForm.ButtonMatrixIntegerClick(Sender: TObject); 87 var 88 Matrix: TMatrixInteger; 89 I: Integer; 90 begin 91 ListViewOutput.Clear; 92 WriteOutput('TListInteger test'); 93 Matrix := TMatrixInteger.Create; 94 with Matrix do try 95 WriteOutput('AddMatrix([10, 20, 30, 40])'); 96 //AddMatrix([10, 20, 30, 40]); 97 WriteOutput('Implode: ' + Implode(',', IntToStr)); 67 98 Clear; 68 99 for I := 0 to 10 do Add(I); 69 Write Ln('Implode: ' + Implode(',', IntToStr));70 Write Ln('Count: ' + IntToStr(Count));71 Write Ln('Implode: ' + Implode(',', IntToStr));72 Write Ln('Reverse');100 WriteOutput('Implode: ' + Implode(',', IntToStr)); 101 WriteOutput('Count: Y: ' + IntToStr(Count.Y) + ', ' + IntToStr(Count.X)); 102 WriteOutput('Implode: ' + Implode(',', IntToStr)); 103 WriteOutput('Reverse'); 73 104 Reverse; 74 Write Ln('Implode: ' + Implode(',', IntToStr));75 Write Ln('First: ' + IntToStr(First));76 Write Ln('Last: ' + IntToStr(Last));77 MoveItems(3, 2, 3);78 Write Ln('Implode: ' + Implode(',', IntToStr));79 Write Ln('Insert(5, 11)');80 Insert(5, 11);81 Write Ln('Implode: ' + Implode(',', IntToStr));105 WriteOutput('Implode: ' + Implode(',', IntToStr)); 106 WriteOutput('First: ' + IntToStr(First)); 107 WriteOutput('Last: ' + IntToStr(Last)); 108 //MoveItems(3, 2, 3); 109 WriteOutput('Implode: ' + Implode(',', IntToStr)); 110 WriteOutput('Insert(5, 11)'); 111 //Insert(5, 11); 112 WriteOutput('Implode: ' + Implode(',', IntToStr)); 82 113 finally 83 114 Free; … … 95 126 I: Integer; 96 127 begin 97 MemoOutput.Clear;98 Write Ln('TListObject test');128 ListViewOutput.Clear; 129 WriteOutput('TListObject test'); 99 130 List := TListObject.Create; 100 131 with List do try 101 132 AddArray([TObject.Create, TObject.Create, TObject.Create, TObject.Create]); 102 Write Ln('Implode: ' +Implode(',', ObjectToStr));133 WriteOutput('AddArray([TObject.Create, TObject.Create, TObject.Create, TObject.Create])', Implode(',', ObjectToStr)); 103 134 Clear; 135 WriteOutput('Clear', Implode(',', ObjectToStr)); 104 136 for I := 0 to 10 do Add(TObject.Create); 105 WriteLn('Implode: ' + Implode(',', ObjectToStr)); 106 WriteLn('Count: ' + IntToStr(Count)); 107 WriteLn('Implode: ' + Implode(',', ObjectToStr)); 108 WriteLn('Reverse'); 137 WriteOutput('for I := 0 to 10 do Add(TObject.Create)', Implode(',', ObjectToStr)); 138 WriteOutput('Count', IntToStr(Count)); 109 139 Reverse; 110 Write Ln('Implode: ' +Implode(',', ObjectToStr));140 WriteOutput('Reverse', Implode(',', ObjectToStr)); 111 141 MoveItems(3, 2, 3); 112 Write Ln('Implode: ' +Implode(',', ObjectToStr));142 WriteOutput('MoveItems(3, 2, 3)', Implode(',', ObjectToStr)); 113 143 finally 114 144 Free; … … 121 151 I: Integer; 122 152 begin 123 MemoOutput.Clear;124 Write Ln('TQueueInteger test');153 ListViewOutput.Clear; 154 WriteOutput('TQueueInteger test'); 125 155 Queue := TQueueInteger.Create; 126 156 with Queue do try … … 128 158 Enqueue(2); 129 159 Enqueue(3); 130 WriteLn('Implode: ' + List.Implode(',', IntToStr)); 131 WriteLn('Enqueue: 4'); 160 WriteOutput('Enqueue(1),Enqueue(2),Enqueue(3) ', List.Implode(',', IntToStr)); 132 161 Enqueue(4); 133 Write Ln('Implode: ' +List.Implode(',', IntToStr));134 Write Ln('Dequeue: ' +IntToStr(Dequeue));135 Write Ln('Implode: ' +List.Implode(',', IntToStr));162 WriteOutput('Enqueue(4)', List.Implode(',', IntToStr)); 163 WriteOutput('Dequeued item', IntToStr(Dequeue)); 164 WriteOutput('Dequeue', List.Implode(',', IntToStr)); 136 165 finally 137 166 Free; … … 148 177 Dictionary: TDictionaryStringString; 149 178 begin 150 MemoOutput.Clear;151 Write Ln('TDictionaryString test');179 ListViewOutput.Clear; 180 WriteOutput('TDictionaryString test'); 152 181 Dictionary := TDictionaryStringString.Create; 153 182 with Dictionary do try … … 155 184 Add('Key2', 'Value2'); 156 185 Add('Key3', 'Value3'); 157 Write Ln('Implode: ' +Implode(',', StringPairToStr));158 Write Ln('Values[Key2]: ' +Values['Key2']);159 Write Ln('Values[Key2] = None');186 WriteOutput('Add(''Key1'', ''Value1''),Add(''Key1'', ''Value1''),Add(''Key1'', ''Value1'')', Implode(',', StringPairToStr)); 187 WriteOutput('Values[Key2]', Values['Key2']); 188 WriteOutput('Values[Key2] = None'); 160 189 Values['Key2'] := 'None'; 161 Write Ln('Values[Key2]: ' +Values['Key2']);162 Write Ln('Values[Key0]: ' +Values['Key0']);163 Write Ln('Keys[2]: ' +Keys[2]);190 WriteOutput('Values[Key2]', Values['Key2']); 191 WriteOutput('Values[Key0]', Values['Key0']); 192 WriteOutput('Keys[2]', Keys[2]); 164 193 finally 165 194 Free; … … 176 205 List: TListChar; 177 206 begin 178 MemoOutput.Clear;179 Write Ln('TListChar test');207 ListViewOutput.Clear; 208 WriteOutput('TListChar test'); 180 209 List := TListChar.Create; 181 210 with List do try 182 211 AddArray([' ', ' ', 'A', 'b', 'c', 'd', ' ']); 183 WriteLn('Implode: ''' + Implode('', CharToStr) + ''''); 184 WriteLn('Implode: ' + Implode('', CharToStr)); 185 WriteLn('Reverse'); 212 WriteOutput('AddArray(['' '', '' '', ''A'', ''b'', ''c'', ''d'', '' ''])', 213 '''' + Implode('', CharToStr) + ''''); 186 214 Reverse; 187 WriteLn('Implode: ''' + Implode('', CharToStr) + ''''); 188 WriteLn('TrimLeft'); 215 WriteOutput('Reverse', '''' + Implode('', CharToStr) + ''''); 189 216 TrimLeft; 190 WriteLn('Implode: ''' + Implode('', CharToStr) + ''''); 191 WriteLn('TrimRight'); 217 WriteOutput('TrimLeft', '''' + Implode('', CharToStr) + ''''); 192 218 TrimRight; 193 WriteLn('Implode: ''' + Implode('', CharToStr) + ''''); 194 WriteLn('UpperCase'); 219 WriteOutput('TrimRight', '''' + Implode('', CharToStr) + ''''); 195 220 UpperCase; 196 WriteLn('Implode: ''' + Implode('', CharToStr) + ''''); 221 WriteOutput('UpperCase', '''' + Implode('', CharToStr) + ''''); 222 LowerCase; 223 WriteOutput('LowerCase', '''' + Implode('', CharToStr) + ''''); 197 224 finally 198 225 Free; … … 207 234 I: Integer; 208 235 begin 209 MemoOutput.Clear;236 ListViewOutput.Clear; 210 237 try 211 238 List := TListPointer.Create; 212 WriteLn('TListPointer...'); 239 List2 := TList.Create; 240 213 241 StartTime := Now; 214 242 repeat 215 243 List.Add(1); 216 244 until (Now - StartTime) > OneSecond; 217 Write Ln('Add: ' +IntToStr(List.Count) + ' ops/sec');245 WriteOutput('TListPointer.Add', IntToStr(List.Count) + ' ops/sec'); 218 246 List.Clear; 219 247 Application.ProcessMessages; … … 221 249 StartTime := Now; 222 250 repeat 251 List2.Add(1); 252 until (Now - StartTime) > OneSecond; 253 WriteOutput('TList.Add', IntToStr(List2.Count) + ' ops/sec'); 254 List2.Clear; 255 Application.ProcessMessages; 256 257 StartTime := Now; 258 repeat 223 259 List.Insert(0, 1); 224 260 until (Now - StartTime) > OneSecond; 225 Write Ln('Insert: ' +IntToStr(List.Count) + ' ops/sec');261 WriteOutput('TListPointer.Insert', IntToStr(List.Count) + ' ops/sec'); 226 262 List.Clear; 227 263 Application.ProcessMessages; 228 264 265 StartTime := Now; 266 repeat 267 List2.Insert(0, 1); 268 until (Now - StartTime) > OneSecond; 269 WriteOutput('TList.Insert', IntToStr(List2.Count) + ' ops/sec'); 270 List2.Clear; 271 Application.ProcessMessages; 272 229 273 for I := 0 to 1000000 do 230 274 List.Add(1); … … 235 279 Inc(I); 236 280 until (Now - StartTime) > OneSecond; 237 Write Ln('Delete: ' +IntToStr(I) + ' ops/sec');281 WriteOutput('TListPointer.Delete', IntToStr(I) + ' ops/sec'); 238 282 List.Clear; 239 283 Application.ProcessMessages; 240 284 241 285 for I := 0 to 1000000 do 286 List2.Add(1); 287 StartTime := Now; 288 I := 0; 289 repeat 290 List2.Delete(0); 291 Inc(I); 292 until (Now - StartTime) > OneSecond; 293 WriteOutput('TList.Delete', IntToStr(I) + ' ops/sec'); 294 Application.ProcessMessages; 295 296 for I := 0 to 1000000 do 242 297 List.Add(1); 243 298 StartTime := Now; … … 247 302 Inc(I); 248 303 until (Now - StartTime) > OneSecond; 249 Write Ln('Move: ' +IntToStr(I) + ' ops/sec');304 WriteOutput('TListPointer.Move', IntToStr(I) + ' ops/sec'); 250 305 List.Clear; 251 306 Application.ProcessMessages; 252 307 253 308 for I := 0 to 1000000 do 309 List2.Add(1); 310 StartTime := Now; 311 I := 0; 312 repeat 313 List2.Move(300000, 700000); 314 Inc(I); 315 until (Now - StartTime) > OneSecond; 316 WriteOutput('TList.Move', IntToStr(I) + ' ops/sec'); 317 Application.ProcessMessages; 318 319 for I := 0 to 1000000 do 254 320 List.Add(1); 255 321 StartTime := Now; … … 259 325 Inc(I); 260 326 until (Now - StartTime) > OneSecond; 261 Write Ln('Exchange: ' +IntToStr(I) + ' ops/sec');327 WriteOutput('TListPointer.Exchange', IntToStr(I) + ' ops/sec'); 262 328 List.Clear; 263 329 Application.ProcessMessages; 264 330 265 331 for I := 0 to 1000000 do 332 List2.Add(1); 333 StartTime := Now; 334 I := 0; 335 repeat 336 List2.Exchange(300000, 700000); 337 Inc(I); 338 until (Now - StartTime) > OneSecond; 339 WriteOutput('TList.Exchange', IntToStr(I) + ' ops/sec'); 340 Application.ProcessMessages; 341 342 for I := 0 to 1000000 do 266 343 List.Add(1); 267 344 StartTime := Now; … … 271 348 Inc(I); 272 349 until (Now - StartTime) > OneSecond; 273 Write Ln('IndexOf: ' +IntToStr(I) + ' ops/sec');350 WriteOutput('TListPointer.IndexOf', IntToStr(I) + ' ops/sec'); 274 351 List.Clear; 275 352 Application.ProcessMessages; 353 354 for I := 0 to 1000000 do 355 List2.Add(1); 356 StartTime := Now; 357 I := 0; 358 repeat 359 List2.IndexOf(Pointer(I mod List2.Count)); 360 Inc(I); 361 until (Now - StartTime) > OneSecond; 362 WriteOutput('TList.IndexOf', IntToStr(I) + ' ops/sec'); 363 Application.ProcessMessages; 364 276 365 finally 277 366 List.Free; 278 end;279 280 try281 List2 := TList.Create;282 WriteLn;283 WriteLn('Test TList...');284 StartTime := Now;285 repeat286 List2.Add(1);287 until (Now - StartTime) > OneSecond;288 WriteLn('Add: ' + IntToStr(List2.Count) + ' ops/sec');289 List2.Clear;290 Application.ProcessMessages;291 292 StartTime := Now;293 repeat294 List2.Insert(0, 1);295 until (Now - StartTime) > OneSecond;296 WriteLn('Insert: ' + IntToStr(List2.Count) + ' ops/sec');297 List2.Clear;298 Application.ProcessMessages;299 300 for I := 0 to 1000000 do301 List2.Add(1);302 StartTime := Now;303 I := 0;304 repeat305 List2.Delete(0);306 Inc(I);307 until (Now - StartTime) > OneSecond;308 WriteLn('Delete: ' + IntToStr(I) + ' ops/sec');309 Application.ProcessMessages;310 311 for I := 0 to 1000000 do312 List2.Add(1);313 StartTime := Now;314 I := 0;315 repeat316 List2.Move(300000, 700000);317 Inc(I);318 until (Now - StartTime) > OneSecond;319 WriteLn('Move: ' + IntToStr(I) + ' ops/sec');320 Application.ProcessMessages;321 322 for I := 0 to 1000000 do323 List2.Add(1);324 StartTime := Now;325 I := 0;326 repeat327 List2.Exchange(300000, 700000);328 Inc(I);329 until (Now - StartTime) > OneSecond;330 WriteLn('Exchange: ' + IntToStr(I) + ' ops/sec');331 Application.ProcessMessages;332 333 for I := 0 to 1000000 do334 List2.Add(1);335 StartTime := Now;336 I := 0;337 repeat338 List2.IndexOf(Pointer(I mod List.Count));339 Inc(I);340 until (Now - StartTime) > OneSecond;341 WriteLn('IndexOf: ' + IntToStr(I) + ' ops/sec');342 Application.ProcessMessages;343 344 finally345 367 List2.Free; 346 368 end; … … 355 377 R: string; 356 378 begin 357 MemoOutput.Clear;379 ListViewOutput.Clear; 358 380 try 359 381 Dictionary := TDictionaryStringString.Create; 360 WriteLn('TDictionaryStringString...');361 I := 0;362 StartTime := Now;363 repeat364 Dictionary.Add(IntToStr(I), IntToStr(I));365 I := I + 1;366 until (Now - StartTime) > OneSecond;367 WriteLn('Add pair: ' + IntToStr(Dictionary.Count) + ' ops/sec');368 Application.ProcessMessages;369 370 I := 0;371 StartTime := Now;372 repeat373 R := Dictionary.Values[IntToStr(I mod Dictionary.Count)];374 I := I + 1;375 until (Now - StartTime) > OneSecond;376 WriteLn('Values: ' + IntToStr(I) + ' ops/sec');377 Application.ProcessMessages;378 379 I := 0;380 StartTime := Now;381 repeat382 R := Dictionary.Keys[I mod Dictionary.Count];383 I := I + 1;384 until (Now - StartTime) > OneSecond;385 WriteLn('Keys: ' + IntToStr(I) + ' ops/sec');386 Application.ProcessMessages;387 388 I := 0;389 StartTime := Now;390 repeat391 R := Dictionary.Items[I mod Dictionary.Count].Value;392 I := I + 1;393 until (Now - StartTime) > OneSecond;394 WriteLn('Values by index: ' + IntToStr(I) + ' ops/sec');395 Application.ProcessMessages;396 finally397 Dictionary.Free;398 end;399 400 try401 382 Dictionary2 := TStringList.Create; 402 383 Dictionary2.NameValueSeparator := '|'; 403 WriteLn; 404 WriteLn('TStringList...'); 384 385 I := 0; 386 StartTime := Now; 387 repeat 388 Dictionary.Add(IntToStr(I), IntToStr(I)); 389 I := I + 1; 390 until (Now - StartTime) > OneSecond; 391 WriteOutput('TDictionaryStringString.Add', IntToStr(Dictionary.Count) + ' ops/sec'); 392 Application.ProcessMessages; 393 405 394 I := 0; 406 395 StartTime := Now; … … 409 398 I := I + 1; 410 399 until (Now - StartTime) > OneSecond; 411 WriteLn('Add pair: ' + IntToStr(Dictionary2.Count) + ' ops/sec'); 400 WriteOutput('TStringList.Add', IntToStr(Dictionary2.Count) + ' ops/sec'); 401 Application.ProcessMessages; 402 403 I := 0; 404 StartTime := Now; 405 repeat 406 R := Dictionary.Values[IntToStr(I mod Dictionary.Count)]; 407 I := I + 1; 408 until (Now - StartTime) > OneSecond; 409 WriteOutput('TDictionaryStringString.Values', IntToStr(I) + ' ops/sec'); 412 410 Application.ProcessMessages; 413 411 … … 418 416 I := I + 1; 419 417 until (Now - StartTime) > OneSecond; 420 WriteLn('Values: ' + IntToStr(I) + ' ops/sec'); 418 WriteOutput('TStringList.Values', IntToStr(I) + ' ops/sec'); 419 Application.ProcessMessages; 420 421 I := 0; 422 StartTime := Now; 423 repeat 424 R := Dictionary.Keys[I mod Dictionary.Count]; 425 I := I + 1; 426 until (Now - StartTime) > OneSecond; 427 WriteOutput('TDictionaryStringString.Keys', IntToStr(I) + ' ops/sec'); 421 428 Application.ProcessMessages; 422 429 … … 427 434 I := I + 1; 428 435 until (Now - StartTime) > OneSecond; 429 WriteLn('Keys: ' + IntToStr(I) + ' ops/sec'); 436 WriteOutput('TStringList.Keys(Names)', IntToStr(I) + ' ops/sec'); 437 Application.ProcessMessages; 438 439 I := 0; 440 StartTime := Now; 441 repeat 442 R := Dictionary.Items[I mod Dictionary.Count].Value; 443 I := I + 1; 444 until (Now - StartTime) > OneSecond; 445 WriteOutput('TDictionaryStringString.Items', IntToStr(I) + ' ops/sec'); 430 446 Application.ProcessMessages; 431 447 … … 436 452 I := I + 1; 437 453 until (Now - StartTime) > OneSecond; 438 WriteLn('Values by index: ' + IntToStr(I) + ' ops/sec'); 439 Application.ProcessMessages; 440 finally 454 WriteOutput('TStringList.Items(ValueFromIndex)', IntToStr(I) + ' ops/sec'); 455 Application.ProcessMessages; 456 457 finally 458 Dictionary.Free; 441 459 Dictionary2.Free; 442 460 end; … … 452 470 List: TListString; 453 471 begin 454 MemoOutput.Clear;455 Write Ln('TListString test');472 ListViewOutput.Clear; 473 WriteOutput('TListString test'); 456 474 List := TListString.Create; 457 475 with List do try 458 476 AddArray(['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven']); 459 Write Ln('Count: ' +IntToStr(Count));460 Write Ln('Implode: ' +Implode(',', StrToStr));461 Write Ln('Reverse');477 WriteOutput('Count', IntToStr(Count)); 478 WriteOutput('Implode', Implode(',', StrToStr)); 479 WriteOutput('Reverse'); 462 480 Reverse; 463 Write Ln('Implode: ' +Implode(',', StrToStr));464 Write Ln('First: ' +First);465 Write Ln('Last: ' +Last);481 WriteOutput('Implode', Implode(',', StrToStr)); 482 WriteOutput('First', First); 483 WriteOutput('Last', Last); 466 484 MoveItems(2, 3, 3); 467 Write Ln('Implode: ' +Implode(',', StrToStr));485 WriteOutput('Implode', Implode(',', StrToStr)); 468 486 finally 469 487 Free; … … 475 493 end; 476 494 477 procedure TMainForm.WriteLn(Text: string = ''); 478 begin 479 MemoOutput.Lines.Add(Text); 495 procedure TMainForm.WriteOutput(Text1: string = ''; Text2: string = ''); 496 var 497 NewItem: TListItem; 498 begin 499 NewItem := ListViewOutput.Items.Add; 500 NewItem.Caption := Text1; 501 NewItem.SubItems.Add(Text2); 480 502 end; 481 503
Note:
See TracChangeset
for help on using the changeset viewer.