Delphi FMX TListBox 处理大型列表时速度较慢

Mik*_*kup 2 delphi firemonkey tlistbox delphi-10.4-sydney

我正在将 2,000 个名称加载到 FMX TListBox 中,但花费的时间太长(例如 35 秒或更长)。

这是测试代码:

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;
Run Code Online (Sandbox Code Playgroud)

TListBox 是否有什么东西使得它对于几千个字符串来说太慢了?

Ken*_*ite 5

使用BeginUpdate后,EndUpdate我的系统运行时间从 25 秒减少到约 125 毫秒。

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;
Run Code Online (Sandbox Code Playgroud)

  • `Clear()` 也应该位于 `(Begin|End)Update` 对内。 (3认同)
  • @AndreasRejbrand:出于同样的原因,我不小心在“BeginUpdate”之前留下了“Clear”。:-) 固定的。谢谢。 (2认同)