有什么优点和使用记录与从TList&LT动态数组的利弊; TMyRecord&GT;在德尔福? [英] What are the pros and cons of using dynamic array of records vs. TList<TMyRecord> in Delphi?
本文介绍了有什么优点和使用记录与从TList&LT动态数组的利弊; TMyRecord&GT;在德尔福?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是为了产生德尔福优点和不同的数据存储方式利弊一个查找列表中的理论问题。
This is a theoretical question intended to generate a look-up list of pros and cons of different data storage ways in Delphi.
让我们说我们有一个记录:
Let's say we have a record:
type
TMyRecord = record
X,Y,Z: Single;
IsValid: Boolean;
end;
存储这些记录阵列的基本选项是:
Basic options of storing an array of such records are:
-
TMyRecord的阵列;
-
从TList
用的getter / setter定制后裔 -
&的TList LT; TMyRecord取代;
array of TMyRecord;
- custom descendant of
TList
with getter/setter TList<TMyRecord>;
我对#1#VS 3比较特别感兴趣,有多少是那些之间的差异,尤其是性能明智的。
I'm specifically interested about #1 vs #3 comparison, how much is the difference between those, especially performance-wise.
推荐答案
&的TList LT; T&GT;
优点:
- array中没有添加/插入/删除/排序/搜索,做的TList有用的方法。
- 从TList有通知的方法,可以被重写,以执行对项目添加/删除一些自定义操作。
&的TList LT; T&GT;
缺点:
-
&的TList LT; T&GT; [我]
的实际返回的复制它的元素的。所以你不能写东西像&的TList LT; TMyRec&GT; IDX] .SomeField:= foo的
。相反,你必须使用临时变量。显然阵列允许这样的前pression。
的大卫·赫弗南提到&的TList LT; T&GT;的.List
从而消除这一缺点;但是,它只出现在XE3 的 - 从TList是应该在程序完成时,没有必要。 删除
- System.Generics.Collections单元可以补充充足的二进制文件的大小来,这不是之前使用System.Classes单位的一个项目。
TList<T>[i]
actually returns a copy of its element. So you can't write something likeTList<TMyRec>[idx].SomeField := foo
. Instead, you have to use temporary variable. Array obviously allows such expression. David Heffernan mentionedTList<T>.List
which eliminates this drawback; however, it appeared only in XE3- TList is an object which should be deleted
at program finishwhen not needed. - System.Generics.Collections unit could add plenty of binary size to a project that wasn't using System.Classes unit before.
至于我自己,我写了 TRecordList&LT; T&GT;
这工作项目,如指针(如经典做的TList)班
For myself I wrote TRecordList<T>
class which operates items as pointers (like classic TList does).
这篇关于有什么优点和使用记录与从TList&LT动态数组的利弊; TMyRecord&GT;在德尔福?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文