Ignore:
Timestamp:
Oct 30, 2010, 7:31:20 PM (14 years ago)
Author:
george
Message:
  • Added: Benchmark for comparasion between classes TStringList and TDictionaryStringString.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Generics/TemplateGenerics/Demo/UMainForm.pas

    r79 r80  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   ComCtrls, ListInteger, ListString, DictionaryString, QueueInteger, ListChar,
    10   ListPointer, DateUtils, ListVariant, Contnrs, ListObject;
     9  ComCtrls, ListInteger, ListString, DictionaryStringString, QueueInteger, ListChar,
     10  ListPointer, DateUtils, ListObject;
    1111
    1212type
     
    1515
    1616  TMainForm = class(TForm)
     17    ButtonBenchmarkDictionary: TButton;
    1718    ButtonListObject: TButton;
    18     ButtonBenchmark: TButton;
     19    ButtonBenchmarkList: TButton;
    1920    ButtonCharList: TButton;
    2021    ButtonQueueInteger: TButton;
     
    2324    ButtonStringList: TButton;
    2425    MemoOutput: TMemo;
    25     procedure ButtonBenchmarkClick(Sender: TObject);
     26    procedure ButtonBenchmarkDictionaryClick(Sender: TObject);
     27    procedure ButtonBenchmarkListClick(Sender: TObject);
    2628    procedure ButtonCharListClick(Sender: TObject);
    2729    procedure ButtonDictionaryStringClick(Sender: TObject);
     
    132134end;
    133135
    134 function StringPairToStr(Pair: TPairString): string;
     136function StringPairToStr(Pair: TPairStringString): string;
    135137begin
    136138  Result := Pair.Key + ':' + Pair.Value;
     
    139141procedure TMainForm.ButtonDictionaryStringClick(Sender: TObject);
    140142var
    141   Dictionary: TDictionaryString;
     143  Dictionary: TDictionaryStringString;
    142144begin
    143145  MemoOutput.Clear;
    144146  WriteLn('TDictionaryString test');
    145   Dictionary := TDictionaryString.Create;
     147  Dictionary := TDictionaryStringString.Create;
    146148  with Dictionary do try
    147149    Add('Key1', 'Value1');
     
    193195end;
    194196
    195 procedure TMainForm.ButtonBenchmarkClick(Sender: TObject);
     197procedure TMainForm.ButtonBenchmarkListClick(Sender: TObject);
    196198var
    197199  List: TListPointer;
     
    336338  finally
    337339    List2.Free;
     340  end;
     341end;
     342
     343procedure TMainForm.ButtonBenchmarkDictionaryClick(Sender: TObject);
     344var
     345  Dictionary: TDictionaryStringString;
     346  Dictionary2: TStringList;
     347  StartTime: TDateTime;
     348  I: Integer;
     349  R: string;
     350begin
     351  MemoOutput.Clear;
     352  try
     353    Dictionary := TDictionaryStringString.Create;
     354    WriteLn('TDictionaryStringString...');
     355    I := 0;
     356    StartTime := Now;
     357    repeat
     358      Dictionary.Add(IntToStr(I), IntToStr(I));
     359      I := I + 1;
     360    until (Now - StartTime) > OneSecond;
     361    WriteLn('Add pair: ' + IntToStr(Dictionary.Count) + ' ops/sec');
     362    Application.ProcessMessages;
     363
     364    I := 0;
     365    StartTime := Now;
     366    repeat
     367      R := Dictionary.Values[IntToStr(I mod Dictionary.Count)];
     368      I := I + 1;
     369    until (Now - StartTime) > OneSecond;
     370    WriteLn('Values: ' + IntToStr(I) + ' ops/sec');
     371    Application.ProcessMessages;
     372
     373    I := 0;
     374    StartTime := Now;
     375    repeat
     376      R := Dictionary.Keys[I mod Dictionary.Count];
     377      I := I + 1;
     378    until (Now - StartTime) > OneSecond;
     379    WriteLn('Keys: ' + IntToStr(I) + ' ops/sec');
     380    Application.ProcessMessages;
     381
     382    I := 0;
     383    StartTime := Now;
     384    repeat
     385      R := Dictionary.Items[I mod Dictionary.Count].Value;
     386      I := I + 1;
     387    until (Now - StartTime) > OneSecond;
     388    WriteLn('Values by index: ' + IntToStr(I) + ' ops/sec');
     389    Application.ProcessMessages;
     390  finally
     391    Dictionary.Free;
     392  end;
     393
     394  try
     395    Dictionary2 := TStringList.Create;
     396    Dictionary2.NameValueSeparator := '|';
     397    WriteLn('TStringList...');
     398    I := 0;
     399    StartTime := Now;
     400    repeat
     401      Dictionary2.Add(IntToStr(I) + Dictionary2.NameValueSeparator + IntToStr(I));
     402      I := I + 1;
     403    until (Now - StartTime) > OneSecond;
     404    WriteLn('Add pair: ' + IntToStr(Dictionary2.Count) + ' ops/sec');
     405    Application.ProcessMessages;
     406
     407    I := 0;
     408    StartTime := Now;
     409    repeat
     410      R := Dictionary2.Values[IntToStr(I mod Dictionary2.Count)];
     411      I := I + 1;
     412    until (Now - StartTime) > OneSecond;
     413    WriteLn('Values: ' + IntToStr(I) + ' ops/sec');
     414    Application.ProcessMessages;
     415
     416    I := 0;
     417    StartTime := Now;
     418    repeat
     419      R := Dictionary2.Names[I mod Dictionary2.Count];
     420      I := I + 1;
     421    until (Now - StartTime) > OneSecond;
     422    WriteLn('Keys: ' + IntToStr(I) + ' ops/sec');
     423    Application.ProcessMessages;
     424
     425    I := 0;
     426    StartTime := Now;
     427    repeat
     428      R := Dictionary2.ValueFromIndex[I mod Dictionary2.Count];
     429      I := I + 1;
     430    until (Now - StartTime) > OneSecond;
     431    WriteLn('Values by index: ' + IntToStr(I) + ' ops/sec');
     432    Application.ProcessMessages;
     433  finally
     434    Dictionary2.Free;
    338435  end;
    339436end;
Note: See TracChangeset for help on using the changeset viewer.