delphifiremonkeytlistboxdelphi-10.4-sydney

Delphi FMX TListBox slow with large lists


I'm loading 2,000 names into an FMX TListBox and it is taking far too long (like 35 seconds or more).

Here is the test code:

procedure TDocWindow.DEBUGAddLotsOfStringsToList;
var
  theTimeAtStart: Integer;
  J: Integer;

begin
  ListBox1.Clear;

  theTimeAtStart := TThread.GetTickCount;

  for J := 1 to 2200 do
    begin
      ListBox1.Items.Add(j.toString);
    end;

  ShowMessage('There were ' + J.ToString + ' strings added to the list in ' + (TThread.GetTickCount - theTimeAtStart).ToString + ' milliseconds.');
end;

Is there something about TListBox that makes it too slow for a few thousand strings?


Solution

  • Using BeginUpdate and EndUpdate reduced the runtime on my system from 25 seconds to about 125 ms.

    procedure TForm1.Button1Click(Sender: TObject);
    var
      theTimeAtStart: Integer;
      J: Integer;
    begin
      theTimeAtStart := TThread.GetTickCount;
    
      ListBox1.Items.BeginUpdate;
    
      try
        ListBox1.Clear;
    
        for J := 1 to 2200 do
        begin
          ListBox1.Items.Add(j.ToString());
        end;
      finally
        ListBox1.Items.EndUpdate;
      end;
      ShowMessage('There were ' + J.ToString + ' strings added to the list in ' + (TThread.GetTickCount - theTimeAtStart).ToString + ' milliseconds.');
    end;