Can I add another list to your list?
If you do not use the inherit function for data in TAtom , you can use record instead of class . Each instance of the class must be allocated to memory, filled with zero and initialized separately. Getmem/Freemem always stands, and memory fragmentation increases.
A pre-allocated dynamic array of record will be faster than individual instances of the class to add. And the data is better for the L1 / L2 CPU cache.
For insertion and deletion, an array of such records will be slower than TList if you have a huge number of elements, because there will be more data to delete / insert ( TList/TObjectList as supporting only a list of pointers). For even faster insertion / deletion, you better use a linked list.
The TList/TObjectList mechanism has some overhead due to internal notification. mechanism And the GetItem() property may be slightly slower (due to range checking) than using a dynamic array directly.
But with our TDynArray wrapper, you can stick with a dynamic array and still have good performance, pre-allocation functions, and TList methods. And even more methods available, such as SaveToStream, Slice, Reverse , sorting with external indexes, etc.
type TAtom = record // could be 'packed record' to save memory (but loose perf) ElementZ: Integer; X, Y, Z: Extended; other variables: other types; // TDynArray also handle complex (eg string) types here end; TAtoms = array of TAtom; var Atom: TAtom; AtomArray: TAtoms; AtomCount: integer; Atoms: TDynArray; begin Atoms.Init(TypeInfo(TAtoms),AtomArray,@AtomCount); Atoms.Capacity := 10000; // pre-allocate array = same as SetLength(AtomArray,10000) for i := 1 to 10000 do begin A.ElementZ := Random(1000); AX := Random; AY := Ramdom; AZ := Random; // set other fields Atoms.Add(A); // fast adding of A properties end; // you have TList-like methods for your dynamic array Atoms.Delete(500); // delete 500th item A.ElementZ := 5000; Atoms.Insert(500,A); // insert A values at 500th index assert(Atoms.Count=10000); assert(AtomCount=10000); // same as Atoms.Count Atoms.Compare := SortDynArrayInteger; Atoms.Sort; // will sort by 1st Integer value = ElementZ for i := 1 to Atoms.Count-1 do // or AtomCount-1 // you have still direct access to AtomArray[] // -> this is even the fastest access to the data assert(AtomArray[i].ElementZ >=AtomArray[i-1].ElementZ ) Atoms.SaveToStream(aStream); // will also save any string content Atoms.Reverse; // reverse all items order Atoms.Clear; // faster adding will be done with direct access to the dynamic array Atom.Count := 10000; // allocate memory for 10000 items for i := 0 to 10000-1 do with AtomArray[i] do begin ElementZ := Random(2000); X := Random; Y := Random; Z := Random; end; Atoms.Sort; // TDynArray knows about the data just created end; // no need to have any try...finally ..Free block
Works with Delphi 6 to XE.
With a newer version of Delphi that supports generics, you'd better go in that direction.