views:

65

answers:

4

I have a dictionary of 10000 Product/Colour/Size combinations which I have created with 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 have to sync up my database with the company's ERP every 30 minutes, and for every Colour/Size combo I need to use this dictionary to add, edit or delete records. I wish they supplied ID numbers.

I don't know how Dictionary works internally. How quick is .NET at finding the right value based on such a key? Should I sort the database query, or does .NET have some other way of identifying the key?

Or should I convert it into a List and use a Dictionary to identify the right index? Or another way completely?

I also use Static Dictionaries in this way all over website's application, so learning a better way of doing this would have quite an influence.

Many thanks, Steve

+1  A: 

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

HCL
+3  A: 

For what you're doing the dictionary is perfect.

The retrieval time on keys for items in a dictionary is damn fast, but ultimately relies on the hash code function of the key (in your case string.GetHashCode()).

You're in luck, because the .Net string's GetHashCode() function is very good. If you do get a hash code clash, .Net will call the Equals method on the object, and so which guarantees uniqueness.

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

Sorting the result-set from the database will be of no benefit in this case.

Hope this helps.

Binary Worrier
+2  A: 

How quick is .NET at finding the right value based on such a key?

The complexity of retrieving a value for a key is close to O(1) according to MSDN, so it's pretty fast...

Also from MSDN:

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

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

Thomas Levesque
Thomas, apologies for being pedantic, but that an operation has low complexity, surely doesn't mean it's intrinsically fast. It'll be faster than an operation with high complexity, but if the underlying algorithm is complex, the big O notation can be low, but the operation can still be slow. Or am I talking out of my arse? (I'm no expert on the Big O)
Binary Worrier
You're right, the complexity is not the only thing to take into account, but non-constant complexity would have a major impact on the actual retrieval speed as the Dictionary grows. It can only be that fast because the complexity is O(1)
Thomas Levesque
I jumped into programming with the relatively high-level languages and have never had to worry about sorting-algorithms.. I'm pleased I don't need to now. Thanks for your answers.
StevieMcG
+1: Thanks mate.
Binary Worrier
A: 

I use this 'pattern' quite frequently, if you cant get SQL queries (especially on SQL CE) to run fast enough.

You may want to look at the ToLookup function too, as I find it more convenient in the majority of cases. Lookup speeds are not affected, it uses a Dictionary mapped to collections.

leppie
ToLookup() is also going to help immensely in the future, thanks.
StevieMcG