.NET Zone is brought to you in partnership with:

Robert is a South African technology specialist with focus on Microsoft technologies. He is very passionate about teaching and sharing and is a Microsoft MVP & Ranger. Robert is a DZone MVB and is not an employee of DZone and has posted 75 posts at DZone. You can read more from them at their website. View Full User Profile

MythBusters: Arrays Versus Collections

07.07.2012
| 3212 views |
  • submit to reddit

The myths I want to tackle are:

  • Arrays are not collections
  • Arrays are faster than collections

These are two statements I have heard recently and I disagree completely. I am assuming when people say collections they mean classes in System.Collections or System.Collections.Generics.

tl;dr: Both are busted.

Arrays are not collections

In computer science, an array type is a data type that is meant to describe a collection of elements

Source: http://en.wikipedia.org/wiki/Array_data_type

So we have established that they are collections but how to they compare to say .NET collection classes like List<T>, ArrayList & LinkedList<T>?

List<T>

What it implements

The first aspect is what interfaces are implemented in each, and you can see below both implement the same interfaces that deal with collections and lists. Note that since List<T> is generic we have both generic & non-generic interfaces.

Where is the data?

Where does List>T? store data? In an internal field: _items which is just an array of T.

So List<T> is just an array? Yup! So how does it handle inserting new items? It increases the array size when needed by creating a new array and coping the items into it.

ArrayList

Let me warn you about ArrayList, if you are using .NET 2.0 or higher and you are using this class, I have the full right to smack you across the face with a glove. There is NO REASON TO USE THIS ANYMORE! Use List<T> instead, it is faster & safer.

What it implements

The first aspect is what interfaces are implemented in each, and you can see below both implement the same interfaces that deal with collections and lists.

Where is the data?

Where does ArrayList store data? In an internal field: _items which is just an object[].

So ArrayList is just an array? Yup! So how does it handle inserting new items? It increases the array size when needed by creating a new array and coping the items into it.

Did you copy and paste List<T> for ArrayList?

Yip – because they are virtually the same, in fact the only core difference is that the internal storage is object[] versus T[]. That has side effects, like the Add methods taking in T versus object but that is it!

LinkedList<T>

So now we are onto something really different. An Array (and as we have seen ArrayList & List<T>) assign a continuous block of memory (number of items * item size) and then put the items in there are the correct offset.

This has the benefit for reading, for example if I need item 6, all I do is work out 6 * item size + start of array in memory and boom I am at the item. However, the problem is when I run out of space – List<T> & ArrayList solve this by making new bigger arrays! The cost of this is very high indeed (two arrays, having to copy all the items from one array to another, cleanup of the first array).

LinkedList is different, it knows about the first item in the collection, and that item has a property to the next item in the collection and so on. This means that I can add infinitely and never have issues as all I do is get the last item, and set it’s property to point to the my new item. Inserts are also way faster compared to arrays – rather than having to move all items down (as Arrays must) we just need to adjust the properties of the item before & the item I am inserting. So it is awesome? Nope – Reading is way slower.

If I need to get to item 6? I need to go to item 1, then item 2 and so on – moving along the entire way. It is slow, VERY much so. However moving backwards may be faster! This is because the node has not only a property that shows the next item but also a property that shows the previous item. So if I need to move one back it is very quick and easy, compare to arrays where I need to go back to the start and apply some math. Performance of moving backwards will really depend on how many steps backwards & the size, so in some cases array may still be faster.

What it implements

The first aspect is what interfaces are implemented in each, and you can see below both implement the same interfaces that deal with collections but not lists. So it is a collection, in the .NET sense, but not a list. It also contains the generic and non-generic versions.

Where is the data?

LinkedList<T> stores the first item in the list in a field called head which is of type LinkedListNode<T> which points to the first node in the collection. That is it, thee rest of the items in the collection are just linked to the head.

Myth Outcome?

BUSTED! Arrays are collections, in both CS & .NET definitions.

Arrays are faster than collections

imageSo let’s see if they are with some micro-benchmarks & the code, if you want to dispute, is below.

I ran 6 tests on Array, ArrayList, List<T>, LinkedList<T> & Dictionary<T,K>

The tests were:

  • If we know we have 1000 items ahead of time – optimise and insert 1000 ints.
  • Insert 1000 ints, but do not preallocate any space more than 4.
  • Read every item in an collection of 100 and validate it.
  • Get the 100th item and validate it.
  • Get the 100th item, and then the 99th item and validate it.
  • Get an item based on a key

Results

Inserting into a known size?

Winner: Array. No surprise here.

LinkedList disqualified because you can’t preallocate.

Dictionary disqualified because it needs a key – we only have values in this test.

Inserting into an unknown size?

Winner: List<T>. Interesting is that I would’ve assumed LinkedList to be faster & ArrayList to be closer to List<T>.

Array disqualified since it can’t magically expand like the others.

Dictionary disqualified because it needs a key – we only have values in this test.

Read every item?

Winner: Array

Dictionary disqualified because it needs a key – we only have values in this test.

Get Item 100?

Winner: ArrayList. Interesting here since I would’ve guessed Array but these are all so close it likely is just background noise interfering.

Dictionary disqualified because it needs a key – we only have values in this test.

Get Item 100 & 99

Winner: Array. This test was designed for LinkedList, so very surprised it didn’t win but these are all so close it likely is just background noise interfering

Dictionary disqualified because it needs a key – we only have values in this test.

Find with a key

Winner: Dictionary. No surprise – it is perfect for this situation.

Raw Numbers

Test Array ArrayList List<T> LinkedList Dictionary
Known Size Insert Speed 0,006501 0,0298411 0,011984    
Unknown Size Insert Speed   0,0287755 0,015749 0,0333027  
Read Every Item Forward 0,007805 0,0161273 0,01311 0,0351886  
Get Item 100 0,002565 0,0017621 0,002034 0,0028298  
Get Item 100 then Item 99 0,001795 0,0022405 0,00251 0,0029249  
Find Item with Key 0,008669 0,0035498 0,005415 0,0065719 0,0021128

Myth Outcome?

BUSTED! Arrays are fast, taking half the awards for speed, but they are not universally the fastest (depends on situation) and also the other options are so close that performance is likely not the issue when considering which structure to choose.

AttachmentSize
ArrayFighter.zip12.38 KB
Published at DZone with permission of Robert Maclean, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)