Is there a faster TList implementation?

前端 未结 4 1539
遇见更好的自我
遇见更好的自我 2021-01-01 02:11

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

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-01 03:03

    It looks like you're doing a lot of adds. I don't know how many lists that's spread over, but if your individual lists are growing very large, you might want to implement a list that grows faster.

    Take a look at TList.Grow, which is called when you try to add an item to a list where all its array elements are in use, and you'll see that it grows by 25%. This is to keep memory use down to a reasonable level. But if you need really large lists, make your own descendant class and override Grow so that in the second line, instead of Delta := FCapacity div 4 it says Delta := FCapacity. This makes your list grow twice as large each time, which means less reallocs and less copies.

    But the thing that's probably killing you is all those Remove calls. Remove has to find the item before it can remove it, which involves a call to IndexOf, which is a linear scan of the entire array. If you've got a large list and you're doing a lot of removes, that's gonna kill your performance.

    What are you using these lists for, especially the big ones? Depending on what you're doing with them, there may be better data structures for the job.

提交回复
热议问题