performance of arraylist.add versus redim preserve

P

Paul

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>
 
R

Robin Tucker

Totally. Not even worth asking the question ;)

(google for performance of this and you'll see some benchies somewhere -
it's a no brainer).....
 
H

Herfried K. Wagner [MVP]

Paul said:
Off the cuff, does anyone know if arraylist is more efficeint at adding
items to an array than redim preserve?

Yes, it is, especially if the size of the list changes often.
 
J

Jay B. Harlow [MVP - Outlook]

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>
|
|
 
G

Guest

If I know the maximum number of elements that might be added, I dimension an
array first to that size, set the elements of the array to the values then
redim preserve to the size of the actual array needed. This, I think is the
most efficient way if you don't want to change the no. of elements later.
Also, you don't have to cast the element each time you use it later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top