Speed up the display of Delphi list components

If you use a Delphi component like a ListBox, Memo, TreeList, ListView,... and you add or modify a lot of items (lines, nodes,...), the component's performance becomes very slow. This is due to the fact that after each change, it is redrawn on the screen.

Thus, modifying 10000 items of a ListBox causes 10000 redraws, and that takes from several seconds to several minutes, depending on the computer's speed and the complexity of the process. Imagine how slow it can become if the process involves inserting, deleting, modifying and swapping many thousands of items.

But you can speed up things enormously with the following tip:

Call BeginUpdate before making the changes to the items. When all changes are complete, call EndUpdate to show the changes on screen. BeginUpdate and EndUpdate prevent excessive redraws and speed up processing time when items are added, deleted, or inserted.

Here's a simple source code example for using this technique with a ListBox:

  ListBox1.Items.BeginUpdate;
for i := 1 to 10000 do
ListBox1.Items.Add('abcd');
ListBox1.Items.EndUpdate;

To give you an idea of the improvement, we timed the code above (your PC may be faster or slower):
»  without the BeginUpdate/EndUpdate lines: 4.3 seconds...
»  with BeginUpdate/EndUpdate: 0.1 seconds, that's 43 times faster!

Here's a source code example for using this technique with a Memo:

  Memo1.Lines.BeginUpdate;
for i := 1 to 5000 do
Memo1.Lines.Add('abcd');
Memo1.Lines.EndUpdate;

»  without BeginUpdate/EndUpdate: 25 seconds...
»  with BeginUpdate/EndUpdate: 0.6 seconds!

转载于:https://www.cnblogs.com/yunhaisoft/archive/2009/05/07/1452032.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值