Delete (0) from a TList is expensive because all the subsequent items need to be moved down. If I need to delete a large number of items from the start of an even larger list what’s the fastest way?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Deleting a large range of elements from the beginning of a
TListis expensive. Although the class name flatters to deceive, aTListis in fact an array. InTListthere is no facility to delete a range–each item must be deleted individually and then the rest of the list is moved down. For a large range that’s going to provoke an awful lot of reallocations and full list moves.If you had a more modern Delphi you could use the generic list class
TList<T>and avail yourself of theDeleteRangemethod. The documentation includes this vital note:In Delphi 2006 you can write something with equivalent performance characteristics like this: