Is there a faster TList implementation?

前端 未结 4 1557
遇见更好的自我
遇见更好的自我 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 02:45

    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.

提交回复
热议问题