Re: A hint about collections

From: Axel Naumann <>
Date: Tue, 22 Feb 2005 11:08:26 -0600

Hi Alberto,

a TOrdCollection is fine for your purposes. As you point out, the disadvantage is that you don't know the size of the collection beforehand, so the array of pointers to your objects will get re-allocated when growing larger than the original capacity of the collection (set via TOrdCollection's constructor). But that's not such a big deal, and the same would happen with TObjArrays. Adding objects to the end, iterating through two collections is parallel, and deleting objects at the beginning is very efficient with a TOrdCollection.

And did you consider that this:
loop over entries

   get entry for collection A
   get entry for collection B
   do something with A and B

is far more efficient than buffering all objects from the two collections (which seems to be what you're doing):

loop over entries

   get entry for collection A
   add it to collection A
   get entry for collection B
   add it to collection B

loop over entries

   do something with A and B

Another possibility is a TTree: you could have two branches, one for collection A, one for collection B, and just go through all the entries. The advantage would be that you don't have to keep all objects in memory, but only one from each branch - the two objects you're working on.

By the way: no collection will _cause_ memory leaks - but if you don't implement them properly you can get one. Axel.

Alberto Pulvirenti wrote:
> Dear all,
> here I ask for a suggestion about which kind of collection object could
> be better to use.
> I am doing a work where, essentially, I collect many similar objects in
> different collections. After this, I need to process pairs of objects
> coming from different collections in order to produce a result.
> What I know is that every new object which I add to the collection, is
> always put at the end of it (no middle insertions os likely).
> What I DO NOT know is how many objects I will have to manage for each
> collection.
> Given that I need to reduce the CPU time as much as possible, and I must
> check also the memory occupation, I would understand what collection
> object is more suitable for my purposes.
> Up to now, I have worked with TObjArray, but I don't know if this object
> causes memory leaks or just large computing time due to the fact that I
> don't know the number of elements to collect and I must essentially use
> the AddLast() method to a TObjArray which has been initialized with the
> default constructor (without setting any size).
> Reading the web-guide, I found that for a sequential insertion
> procedure, the TOrdCollection is better than TList. The question is: is
> it also better than TObjArray, for a use where I need just to collect
> all objects and then process them sequentially, in the same order as
> they were inserted (eventually deleting from the collection the already
> processed ones, in order to reduce time)?
> Thanks and best regards.
> Alberto
Received on Tue Feb 22 2005 - 18:34:17 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:05 MET