Re: delphi stringlist capacity

Posted by webmaster Guido on March 23, 2007

In Reply to delphi stringlist capacity posted by Johan p14205 on March 22, 2007

: In a Delphi application for full text indexing, I use stringlist objects, doing a lot of adding and modifying items.
: Because the operations take a lot of time (ten thousands of items), I'm trying to optimize the code, but I don't know where the bottleneck is: in the calculations or in adding the stringlist items.
: Somewhere I read that it is faster if you set the capacity of the stringlist before adding items. Is this really effective?

Yes, it is effective, it can cut the time needed by almost 50%. But everything is relative: if the number of added items is small in relation to the speed of the machine and/or the total time needed by your application, you won't notice the effect of the optimization.

Basically, adding an item to a stringlist would involve two steps:

1. Enlarge the table by the necessary space for the additional pointers, or in other words: increase the "capacity" of the stringlist by 1;

2. Put the pointer(s) in this new empty item.

In order to speed things up, Delphi doesn't simply increase the stringlist's capacity by ONE, but by 4, 16, and so on (e.g. when going from 12 to 13 items, the capacity goes from 12 to 28).

If you know approximately how big the stringlist will eventually become, then you can set yourself the capacity large enough so that Delphi doesn't have to increase it many times.

Here's the time measured for adding different numbers of items to a stringlist, measured on an AMD Athlon 3400 with 1 GB of RAM, considered a "low end" machine nowadays:

Millions of   Time in mSec   
items added   "auto" vs. "optimized"
 2               235       140
 4               450       280
 8               890       570
16              2060      1150 
32              4200      2300

Related articles


Follow Ups