unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils, System.Variants, System.Classes, System.UITypes, System.Generics.Collections, System.Generics.Defaults,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls;
type
TBlubb =
class
strict private
FItemValue: Integer;
public
constructor Create(ItemValue: Integer);
property ItemValue: Integer
read FItemValue
write FItemValue;
end;
TfmTest =
class(TForm)
btnStringList: TButton;
btnDictionary: TButton;
edtCount: TEdit;
btnGenerate: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnStringListClick(Sender: TObject);
procedure btnGenerateClick(Sender: TObject);
procedure btnDictionaryClick(Sender: TObject);
private
FDictionary: TObjectDictionary<
string, TBlubb>;
FStringList: TStringList;
procedure Generate;
function Count(StartValue: Cardinal): Cardinal;
public
end;
var
fmTest: TfmTest;
implementation
{$R *.dfm}
procedure TfmTest.FormCreate(Sender: TObject);
begin
FDictionary := TObjectDictionary<
string, TBlubb>.Create([doOwnsValues]);
FStringList := TStringList.Create;
Randomize;
end;
procedure TfmTest.FormDestroy(Sender: TObject);
begin
FDictionary.Free;
FStringList.Free;
end;
procedure TfmTest.Generate;
var
I: Integer;
ItemsCount: Integer;
Value: TBlubb;
begin
FDictionary.Clear;
FStringList.Clear;
ItemsCount := Random(StrToInt(edtCount.Text));
for I := 0
to ItemsCount - 1
do
begin
Value := TBlubb.Create(I);
FDictionary.Add(IntToStr(I), Value);
FStringList.AddObject(IntToStr(I), Value);
end;
MessageDlg(Format('
%d values generiert.', [ItemsCount]), mtInformation, [mbOK], 0);
end;
function TfmTest.Count(StartValue: Cardinal): Cardinal;
begin
Result := GetTickCount - StartValue;
end;
procedure TfmTest.btnGenerateClick(Sender: TObject);
begin
Generate;
end;
procedure TfmTest.btnStringListClick(Sender: TObject);
var
Start: Integer;
SearchKey: Integer;
SearchValue: TBlubb;
ItemPosition: Integer;
begin
Start := GetTickCount;
SearchKey := Random(FStringList.Count - 1);
ItemPosition := FStringList.IndexOf(IntToStr(SearchKey));
SearchValue := TBlubb(FStringList.Objects[ItemPosition]);
MessageDlg(Format('
TStringList: Zeit in Milisekunden (Item # %s, Value: %d): %d', [IntToStr(SearchKey), SearchValue.ItemValue, Count(Start)]), mtInformation, [mbOK], 0);
end;
procedure TfmTest.btnDictionaryClick(Sender: TObject);
var
Start: Integer;
SearchKey: Integer;
SearchValue: TBlubb;
begin
Start := GetTickCount;
SearchKey := Random(FStringList.Count - 1);
FDictionary.TryGetValue(IntToStr(SearchKey), SearchValue);
MessageDlg(Format('
TDictonary: Zeit in Milisekunden (Item # %s, Value: %d): %d', [IntToStr(SearchKey), SearchValue.ItemValue, Count(Start)]), mtInformation, [mbOK], 0);
end;
{ TBlubb }
constructor TBlubb.Create(ItemValue: Integer);
begin
inherited Create;
FItemValue := ItemValue;
end;
end.