This project has moved. For the latest updates, please go here.

ArrayList, Hashmap, Stack vs. List<T>, Dictionary<K,V>, Stack<T>

Topics: Developer Forum
Aug 2, 2009 at 11:24 AM

I am abit puzzled. I modified all the source code (sigh!) from using ArrayList, Hashmap and Stack, into List<T>, Dictionary<K,V> and Stack<T>. But using several profilers, there seem to be very little benefit of doing so. Anyone have suggestions how to benchmark this properly?

Aug 2, 2009 at 12:31 PM

Most of the benefits will not be in performance but in type safety and the LINQ extension methods.

The biggest issue with the old collections vs the generics is having to re-cast objects as you're taking them out. Probably the best way to benchmark it is just do a large iteration stopwatch test while reading data out. Also do some small routines and look at the IL generated.

While explicit casting isn't necessarily slow it does take an extra few steps to do so. That's why it's recommended to use the as-is pattern if you do have to cast with performance in mind. It tells the CLR to skip some checks that are done in an explicit cast (null reference, value type, inheritance). It has one drawback, it only works with reference types. Still over thousands of iterations it will show performance gains. It also eliminates the need to have to catch null reference exceptions.

While I didn't tackle the Stack and HashTable usage I did post my patches for all the LINQ helper methods. I updated to implement IList<T> and use a Dictionary<K,V> where needed.  I did add in tons of helper methods that makes using LINQ much easier.


Aug 2, 2009 at 2:18 PM

Ah, thanks for clearing that up. Makes sense now, don't get how I didn't see it before. Strange.

I've taken notice that you've published your patch, but I haven't had the time to review and test it, but when I do I will give responses in the appropriate Issue/Discussion to keep the history clear.

From what I've noticed, HAP is pretty much optimized as is. However I don't have much experience in the performance increasing field.