Monday, March 22, 2010

Why is the Generic List Faster Than the ArrayList?

Why is the Generic List Faster Than the ArrayList?

There are two reasons:

* The TypeSafe array stores the items in itself rather than pointers to boxed objects
* In a 64 bit OS (like mine), a pointer happens to be 64 bits rather than the 32 bits integer

I won't really expand on this, the things I will remember are:

* Generics are typesafe and fast, there is no need for ArrayList
* Don't use ArrayList on structures or value types (Integers, Float...)

Apart from that, the Insertion of 100,000 items still takes 4 seconds, this is not glorious.
How Could We Make This Faster

It really depends on what you need; you will hardly find a single Collection which will be faster than all others on all the test.

If you use your list to mainly add at the end and access by Index, the List(of Integer) is probably a good choice.

On the contrary, if you require random insertion and can avoid accessing items by index then perhaps a LinkedList(Of Integer) is the way to go.

However, if you need...

* Quick insertion at any position
* Being able to access item by index
* Large lists

No comments:

Post a Comment