加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi 7中的.items.clear之间的区别究竟是什么?

发布时间:2020-12-15 06:10:29 所属栏目:大数据 来源:网络整理
导读:我想知道为什么有两种不同的方法来清除列表.一个是调用listview.clear,其他是listview.items.clear.实际上,这也扩展到许多其他VCL组件.必须使用哪种方法,为什么? 提前致谢 解决方法 ListView.Clear只是ListView.Items.Clear与ListItems.BeginUpdate / ListI
我想知道为什么有两种不同的方法来清除列表.一个是调用listview.clear,其他是listview.items.clear.实际上,这也扩展到许多其他VCL组件.必须使用哪种方法,为什么?

提前致谢

解决方法

ListView.Clear只是ListView.Items.Clear与ListItems.BeginUpdate / ListItems.EndUpdate的封装.看源:
procedure TCustomListView.Clear;
begin
  FListItems.BeginUpdate;
  try
    FListItems.Clear;
  finally
    FListItems.EndUpdate;
  end;
end;

从文档:

The BeginUpdate method suspends screen repainting until the EndUpdate
method is called. Use BeginUpdate to speed processing and avoid
flicker while items are added to or deleted from a collection.

更好的做法是使用BeginUpdate / EndUpdate来提高速度并避免闪烁.
但是使用ListView.Clear的主要原因是因为使用“高级VCL方法”(由@Arnaud评论)总是一个好主意,实现可能会改变(BTW,该方法在D7中引入).

编辑:我已经测试了10k项目的TListView(D7 / WinXP):

> ListView.Items.Clear:?5500 ms
> ListView.Clear:?330 ms

结论:ListView.Clear在不使用BeginUpdate / EndUpdate时比ListView.Items.Clear快16倍!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读