Paul,
In addition to the other comments:
| Off the cuff, does anyone know if arraylist is more efficeint at adding
| items to an array than redim preserve?
If you ReDim Preserve a single element at a time, then ArrayList is far more
efficient. Remember that the ArrayList allocates an internal array with 16
elements, each time it needs to expand this internal array it doubles the
internal array's size.
However if you over allocate your ReDim Preserve by doubling the size of the
array as ArrayList does internally, then performance should be about the
same.
Ideally in both cases you should allocate the number of expected elements in
both cases to prevent needing to resize the array itself or the array
internal to the ArrayList. As this expansion of the array itself or the
ArrayList's internal array is what is hurting performance, as a new array
needs to be allocated & the old array is copied to the new array.
If you over allocate your ReDim Preserve, you might be able to simple do a
final ReDim Preserve when you exit the loop to "return" the array to its
true size.
Also remember if your array handles value types, then using an ArrayList may
hurt performance as the elements will need to be boxed when put into the
array & unboxed when taking out. This boxing of the elements may also cause
extra pressure on the GC.
Hope this helps
Jay
| Off the cuff, does anyone know if arraylist is more efficeint at adding
| items to an array than redim preserve?
|
| Paul
|
| <begin loop>
| Dim c As Integer = SomeArray.GetUpperBound(0) + 1
| ReDim Preserve SomeArray(c)
| SomeArray(c) = SomeObject
| <end loop>
|
| Dim al As ArrayList = ArrayList.Adapter(SomeArray)
| <begin loop>
| al.Add(SomeObject)
| <end loop>
|
|