17

Possible Duplicate:
When to use ArrayList over array[] in c#?

From the perspective of memory or processor costs, does there appear to be a significant difference between an array and an arrayList object?

2
  • @NikhilAgrawal not a duplicate of that topic since the question is specifically about performance differences, not usage context. Commented May 25, 2012 at 1:42
  • I suspect ArrayList would be slower as you have to cast each element in the array when retrieving. Commented May 25, 2012 at 2:00

2 Answers 2

7

An array is a low-level data structure that essentially maps to a region in memory. An ArrayList is a variable length list implemented as an array of object that is re-allocated as the list grows.

ArrayList therefore has some overhead related to managing the size of the internal array, and more overhead related to casting objects to the correct type when you access the list.

Also, storing everything as object means that value types get boxed on write and unboxed on read, which is extremely detrimental to performance. Using List<T>, a similar but strongly-typed variable size list avoids this issue.

In fact, ArrayList is practically deprecated in favor of List<T> since .NET 2.0.

Sign up to request clarification or add additional context in comments.

7 Comments

There is another object called a simple List. How does it compare with an ArrayList. Which appears to be a more sophisticated control.
@MikeOlson I explained that in my answer already... did you read it?
Sorry yes I read it but wasn't sure if the List<T> was the same as the simple list. So basically this simple list has basically replaced the Array list in favor of ease of use?
@MikeOlson Yes List<T> replaces ArrayList because it is strongly typed which avoids the inconvenience, potential bugs and performance concerns of having to cast to object and back.
Thanks that makes sense. Appreciate your time to answer.
|
1

An array is a contiguous block of memory of fixed size, whereas an ArrayList (though you should prefer List since .NET 2.0) wraps an array to provide dynamically-resizable storage.

The "difference" between them being that, as far as they're encapsulated, an ArrayList is resizable, an array isn't. As far as the implementation is concerned: because an ArrayList wraps (and reallocates) arrays it will require more slightly more memory than an array (as it has to know the current number of elements, as opposed to its capacity), furthermore an ArrayList also requires CPU time to reallocate and copy its internal array if it ever reaches its internal capacity.

However, instantiating an ArrayList is no more expensive than allocating an array. The only difference there being the handful of instructions needed to initialize the ArrayList's state. The difference is negligible and not worth worrying about.

You'll find that if you are reallocating an array by yourself as the means of creating a resizable collection then you're better off using ArrayList/List as it has been thoroughly tested.

2 Comments

There is another object called a simple List. How does it compare with an ArrayList. Which appears to be a more sophisticated control.
The main difference between ArrayList and Generic List is that generic list is type safe while arraylist is not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.