TList的快速副本< T> [英] Fast copy of TList <T>?

查看:72
本文介绍了TList的快速副本< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有复制通用TList的快速方法吗?

Is there a fast way to copy a generic TList?

Copy.Capacity := List.Count;
for Item in List do
  Copy.Add (Item);

很慢。似乎没有办法使用 CopyMemory ,因为我不能得到内部数组的内存地址(这从信息隐藏的观点是显而易见的)。我错过了像

is very slow. There seems to be no way to use CopyMemory since I can't get the memory adress of the internal array (which is obvious from an information hiding viewpoint). I miss something like

List.Copy (Copy);

它使用内部表示的知识来提高性能。

对于通用的 TList< T> ,可以做到吗?

which uses the knowledge of the internal representation to improve performance. Can it be done?

推荐答案

它根本不可能实现你想要的功能。这是因为复制 T 的内容可能涉及多个简单的内存复制。如果 T 包含任何托管类型(即字符串,接口等),那么这些托管对象的引用计数必须递增。

For the generic TList<T> it is simply not possible to implement the function you want. That's because copy the contents of T may involve more than a simple memory copy. If T contains any managed types (i.e. strings, interfaces etc.) then the reference counts on those managed objects must be incremented.


  • 如果您的 T 确实包含托管类型,那么我怀疑您可以做得比已经拥有的代码好多少。

  • 如果您的 T 不包含任何托管类型,那么内存副本是可行的,但您需要创建自己的类来封装此列表,因为 TList< T> 不合适。

  • If your T does contain managed types then I doubt that you can do much better then the code you already have.
  • If your T does not contain any managed types then a memory copy is viable but you will need to create your own class to encapsulate this list since TList<T> is not appropriate.

这篇关于TList的快速副本&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆