Code now available at https://github.com/rmaclean/ArrayFighter. If you wish to suggest a flaw or make a suggestion to improve it please create a pull request OR give me a detailed comment explaining what is needed, why it will help/hinder etc... ideally with links.
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 initially felt are wrong, so to check I myself I decided to research this myth. I am assuming when people say collections they mean classes in System.Collections or System.Collections.Generics.
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>
Official Documentation: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
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.
- Array implement: ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
- List<T> implement: IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
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 copying the pointers to the items into it.
Complexity of the operations
- Add: O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.
- Insert: O(n) – where n is count
- Remove: O(n) – where n is count
ArrayList
Official Documentation: http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx
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.
- Array implement: ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
- ArrayList implement: IList, ICollection, IEnumerable, ICloneable
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 copying the pointers to the items into it.
Complexity of the operations
- Add: O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.
- Insert: O(n) – where n is count
- Remove: O(n) – where n is count
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>
Official Documentation: http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx
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.
- Array implement: ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
- LinkedList<T> implement: ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback
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.
Complexity of the operations
- AddFirst: O(1) operation.
- AddLast: O(1) operation.
- AddBefore: O(1) operation.
- AddAfter: O(1) operation.
- Remove: O(n) – where n is count
- RemoveFirst: O(1) operation
- RemoveLast: O(1) operation
Myth Outcome?
BUSTED! Arrays are collections, in both CS & .NET definitions.
Arrays are faster than collections
So 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
- Insert an item at position 0.
Results
9 July 2012: As I added a new test I re-tested everything (so numbers & chart has been updated) – however the winner did not change on the tests.
Inserting into a known size?
Winner: Array
No surprise here.
LinkedList disqualified because you can’t preallocate.
Disqualified
- Dictionary: 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>.
In hindsight it makes sense that LinkedList is slower – it needs to find the last item first as we we are adding at the end.
Disqualified
- Array: Disqualified since it can’t magically expand like the others.
- Dictionary: Needs a key – we only have values in this test.
Read every item?
Winner: Array
Disqualified
- Dictionary: 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.
Disqualified
- Dictionary: 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
No surprise – it is perfect for this situation.
Disqualified
- Dictionary: Needs a key – we only have values in this test.
Find with a key
Winner: Dictionary.
No surprise – it is perfect for this situation.
Insert At Zero
Winner: LinkedList<T>.
No surprise – it is perfect for this situation.
Disqualified
- Array: No way to insert at zero.
- Dictionary: Needs a key – we only have values in this test.
Raw Numbers
Test | Array | ArrayList | List<T> | LinkedList<T> | Dictionary<T,K> |
Known Size Insert Speed | 0,0067952 | 0,0251845 | 0,0125625 | ||
Unknown Size Insert Speed | 0,0322676 | 0,0178365 | 0,0324339 | ||
Read Every Item Forward | 0,0081916 | 0,0135402 | 0,0124702 | 0,03132 | |
Get Item 100 | 0,0023351 | 0,0021007 | 0,0021014 | 0,0033034 | |
Get Item 100 Then Get Item99 | 0,0020214 | 0,0021889 | 0,002163 | 0,0032026 | |
Find Item With Key | 0,0059856 | 0,0042258 | 0,0063673 | 0,0067397 | 0,0026851 |
Insert At Zero | 0,0047753 | 0,0034568 | 0,0024224 |
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.