Compartilhar via


HashTable vs Dictionary Performance

Last week I needed to implement a little complex data structure for our last project. I needed to keep keys in a hashtable like structure for fast access but I also needed a queue structure to remove LRU item since the size in the memory is limited. For the hashtable I used c# hashtable which takes any object as the key and I implemented this. Since the value can be any object I need to do casting when I retrieved objects from hashtable. We had a code review with my lead and he asked why I chose hashtable versus dictionary. A dictionary is more self explanatory since you need to specify types but a hashtable must have better performance, at least I assumed so but he was not sure about that. Anyway when I had time I wrote a small app to test hashtable and dictionary performance in c#. I add 10k guids to both and search for a different set of 10k guids(so that I will have the worst case search result which is not exist). At the end What I found is dictionary has nearly %33 better performance over hashtable but if I change dictionary comparison to case insensitive it did its job in 3x times of hashtable. So if you don’t have case sensitivity issue, dictionary is a better alternative for string key types.