是否有更快的TList实现? [英] Is there a faster TList implementation?

查看:159
本文介绍了是否有更快的TList实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序大量使用了TList,所以我想知道是否有任何替代实现可以针对特定用例进行更快或最优化。



我知道<一个href =http://cc.codegear.com/Item/25537 =nofollow noreferrertitle =RtlVCLOptimize.pas 2.77> RtlVCLOptimize.pas 2.77 ,它优化了几个TList方法的实现。



但是我想知道那里是否有其他的东西。我也不要求它是一个TList的后代,我只需要TList的功能,无论它如何实现。



完全有可能,因为相当基本的功能TList提供,没有太多的改进空间,但仍然希望验证,因此这个问题。$ /

编辑:在我的具体用例中,没有列表被排序。有很多列表,不同数量的元素。我用我自己的类替换了TList,以记录添加/删除调用的数量和元素的数量。它报告(所有列表的toatal):

  ListAdd = 15766012; ListRemove = 10630000; ListCount = 5136012 

我还可以找出单个列表中最高数量的元素。 / p>

我没有特别的问题,我只是想知道有没有办法让这些数字更快,即使小的改进也会相加。

解决方案

我对TList知道的最大瓶颈之一是删除/提取大列表。删除项目[0]比删除项目[Count-1]慢得多,因为它后面的内存移动。



例如,在包含65536个元素的列表:

  while list.Count> 0 do List.Delete(0)//花费2分钟完成

为I:= List.Count-1 downto 0 do List.Delete(I)//少于1秒

所以如果你有一个具有数百万个元素的TList,删除低索引项可能是性价比很高的。另外,考虑到没有排序的列表使得在其中找到一个元素很慢。 IndexOf在大型列表中非常慢。您可能希望考虑将列表排序。



此外,考虑到您的项目数量可能相当大,您可能需要考虑使用TList列表来存储元素,这将有助于减少我已经提到的删除/提取开销。


My application makes heavy use of TList, so I was wondering if there are any alternative implementations that are faster or optimized for particular use case.

I know of RtlVCLOptimize.pas 2.77, which has optimized implementations of several TList methods.

But I'd like to know if there is anything else out there. I also don't require it to be a TList descendant, I just need the TList functionality regardless of how it's implemented.

It's entirely possible, given the rather basic functionality TList provides, that there is not much room for improvement, but would still like to verify that, hence this question.

edit: In my particular use case no lists are sorted. There are lots of lists, with various number of elements in. I did replace TList with my own class in order to log number of Add/Remove calls and count of elements. It reports (toatal for all lists):

ListAdd = 15766012; ListRemove = 10630000; ListCount = 5136012

I could also find out what the highest number of elements in a single list is.

I have no particular issue, I just wonder if there is a way to make it faster all around as with these numbers even small improvement would add up.

解决方案

One of the biggest bottleneck I know about TList is the Delete/Extract on large list. Removing item[0] is a lot slower than removing Item[Count-1] because of the memory move that follows it.

For exemple, on a list containing 65536 elements:

while list.Count > 0 do List.Delete(0) //Takes 2 mins to complete

for I := List.Count-1 downto 0 do List.Delete(I) //Takes less than 1 sec

So if you have a TList with millions of elements, deleting a low index item can be expensive performance-wise. Also, consider that having a list that isn't sorted makes it very slow to find an element in it. IndexOf is very slow on large list. You might want to consider keeping the list sorted.

Also, considering your item count can be pretty large, you might want to consider using a List of TList to store your elements, which will help reduce the Delete/Extract overhead I already mentioned.

这篇关于是否有更快的TList实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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