Search speed for .NET dictionary value by key?

I have a dictionary of 10,000 product / color / size combinations that I created using something like:

AllRecords = DB.ProductColourSizes _ .ToDictionary(function(b) String.Format("{0}_{1}_{2}", _ b.ProductCode, b.ColourCode, b.SizeCode)) 

So, an example key is like "13AI_GRS_M"

I need to synchronize my database with the company's ERP every 30 minutes, and for each color / size combination I need to use this dictionary to add, edit or delete entries. I would like them to provide identification numbers.

I do not know how the dictionary works inside. How fast does .NET find the correct value based on such a key? Should I sort the database query, or does .NET have another way to identify the key?

Or do I need to convert it to a list and use a dictionary to determine the correct index? Or another way?

I also use Static Dictionaries in this way throughout my website app, so learning the best way to do this would have a great impact.

Thanks a lot Steve

+6
performance dictionary lookup
source share
5 answers

What you do is a dictionary perfect.

The search time on the keys for items in the dictionary is very fast, but ultimately depends on the function of the key hash code (in your case string.GetHashCode() ).

You are lucky because the GetHashCode (). Net function is very good. If you get a hash clash, .Net will call the Equals method on the object and thus guarantee uniqueness.

We have dictionaries with hundreds of thousands of items, and search times are negligible.

Sorting the result set from the database will not be useful in this case.

Hope this helps.

+4
source share

How fast does .NET find the correct value based on such a key?

The difficulty of getting the value for the key is close to O (1) according to MSDN , so it is pretty fast ...

Also from MSDN:

The search speed depends on the quality of the hashing algorithm of the type specified for TKey.

If you use a string as a key, you should be fine, as we can assume that the String class uses an efficient hashing algorithm ...

+2
source share

Dictionaries are created to find things, so stay tuned. The main problem for the Key type is that it should have a good (well-distributed) hash code.

You can write your own KeyClass with the ProductCode, ColourCode, and SizeCode elements, but then you have to overload the GetHashCode and Equals elements (and related). And it will be quite difficult to improve GethashCode System.String, and it is quite easy to make mistakes.

So don’t worry. Your key string looks fine.

And if you want to optimize, first profile to see where your problems are.

+1
source share

Finding a value by a key is very fast, and using a dictionary seems absolutely appropriate. The key that you create, it seems to me, is also in order. Presorting the database makes absolutely no sense; the dictionary does not depend on this.

+1
source share

I use this "pattern" quite often if you cannot get SQL queries (especially in SQL CE) to work fast enough.

You might also want to take a look at the ToLookup function, as I find it more convenient in most cases. Search speed is not affected, it uses a dictionary mapped to collections.

0
source share

All Articles