Delphi FMX TListBox速度慢,列表大
我正在将2000个名字加载到FMX TListBox中,这花费的时间太长(比如35秒或更长) 以下是测试代码:Delphi FMX TListBox速度慢,列表大,delphi,firemonkey,tlistbox,delphi-10.4-sydney,Delphi,Firemonkey,Tlistbox,Delphi 10.4 Sydney,我正在将2000个名字加载到FMX TListBox中,这花费的时间太长(比如35秒或更长) 以下是测试代码: procedure TDocWindow.DEBUGAddLotsOfStringsToList; var theTimeAtStart: Integer; J: Integer; begin ListBox1.Clear; theTimeAtStart := TThread.GetTickCount; for J := 1 to 2200 do be
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;
TListBox是否有什么东西使它对于几千个字符串来说太慢了?使用
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;
Clear()
也应该在(Begin | End)Update
对中。@RemyLebeau:已修复。谢谢。当我添加Begin/EndUpdate块时,我没有注意到这一点。@KenWhite:你为什么把BeginUpdate
放在try
之后?@AndreasRejbrand:出于同样的原因,我不小心把Clear
放在BeginUpdate
之前固定的。谢谢。