Einzelnen Beitrag anzeigen

Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#23

AW: TTreeView komige Speedprobleme...

  Alt 28. Aug 2011, 13:08
Ich habe mal zum Vergleich eine ListBox getestet und war doch einigermaßen erstaunt, daß das TreeView (ohne Images) genaus so schnell ist wie diese. Scheint ein Aberglaube zu sein, daß TreeView stinklangsam ist. Auf jeden Fall auch viel schneller als ein Memo, aber das denke ich, ist klar.

Delphi-Quellcode:
procedure GetDirs(const Start: String; const SL: TStrings);
var
  S: TSearchRec;
  P: string;
  R: integer;
begin
  R:= FindFirst(Start+'*.*', faDirectory, S);
  while R = 0 do
  begin
    if ((S.Attr and faDirectory) <> 0) then
      if ((S.Name <> '.') and (S.Name <> '..')) then
      begin
        P:= Start+S.Name+'\';
        SL.Add(P);
        GetDirs(P, SL);
      end;
    R:= FindNext(S);
  end;
  Findclose(S);
end;


procedure GetNodeDirs(const ADirectory: String; var ATree: TTreeView; const Start: TTreeNode);
  function SlashSep(const Path, S: String): String;
  begin
    if AnsiLastChar(Path)^ <> '\then
      Result:= Path+'\'+S
    else
      Result:= Path+S;
  end;
var
  S: TSearchRec;
  N: TTreeNode;
  R: integer;
begin
  R:= FindFirst(SlashSep(ADirectory, '*.*'), faDirectory, S);
  while R = 0 do
  begin
    if ((S.Attr and faDirectory) <> 0) then
      if ((S.Name <> '.') and (S.Name <> '..')) then
      begin
        N:= ATree.Items.AddChild(Start, S.Name);
        GetNodeDirs(SlashSep(ADirectory, S.Name), ATree, N);
      end;
    R:= FindNext(S);
  end;
  Findclose(S);
end;


procedure TForm1.Button1Click(Sender: TObject); // ListBox
var
  fPath: string;
  fTime: Cardinal;
begin
  fPath:= 'C:\';
  ListBox1.Items.Clear;

  fTime:= GetTickCount;
  ListBox1.Items.BeginUpdate;
  GetDirs(fPath, ListBox1.Items);
  ListBox1.Items.EndUpdate;

  Label1.Caption:= IntToStr(GetTickCount-fTime);
end;


procedure TForm1.Button2Click(Sender: TObject); // TreeView
var
  fPath: string;
  fTime: Cardinal;
begin
  fPath:= 'C:\';
  TreeView1.Items.Clear;

  fTime:= GetTickCount;
  TreeView1.Items.BeginUpDate;
  GetNodeDirs(fPath, TreeView1, TreeView1.Items.AddChild(Nil, fPath));
  TreeView1.Items.EndUpDate;

  Label2.Caption:= IntToStr(GetTickCount-fTime);
end;


procedure TForm1.Button3Click(Sender: TObject); // StringList
var
  fPath: string;
  fTime: Cardinal;
  SL: TStringList;
begin
  fPath:= 'C:\';
  SL:= TStringList.Create;

  fTime:= GetTickCount;
  GetDirs(fPath, SL);
  Label3.Caption:= IntToStr(GetTickCount-fTime)+' ('+IntToStr(SL.Count)+')';

  SL.Free;
end;
  Mit Zitat antworten Zitat