SYSK 386: Performance Implications of Using One vs. Several Nested Hashtables

Let’s say you need to quickly find some reference/lookup data and you decide to use hashtables… If your key is comprised of several elements, is it better, from the performance point of view for you to combine all of them into one string and use it to access a flat single hashtable, or, would you be better off with a multi-level lookup?

Without the helper classes, using just hashtables, the two approaches might be represented as follows:

    Option 1 – single flat hashtable: Dictionary<string, decimal> dict1;

    Option 2 – nested hashtables: Dictionary<string, Dictionary<string, Dictionary<int, decimal>>> dict2;

 

My tests show that option 2 is approximately 40-60% faster than option 1 on random lookups.

As always, I suggest you do your own tests with your data and in your environment prior to committing to one pattern over another.