Reputation: 501
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
Upvotes: 6
Views: 2178
Reputation: 51709
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 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.
Upvotes: 6
Reputation: 117220
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.
Upvotes: 0
Reputation: 292345
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...
Upvotes: 2
Reputation: 36765
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.
Upvotes: 1
Reputation: 273169
Dictionaries are made to look up things, so stay with that. The main issue for the Key type is that it should have a good (well distributed) Hash code.
You could write your own KeyClass with ProductCode, ColourCode and SizeCode members but then you would have to overload the GetHashCode and the Equals (and related) members. And it will be pretty hard to improve over the GethashCode of System.String, and pretty easy to make mistakes.
So, don't bother. Your Key string looks OK.
And if you want to optimize, profile first to see where your problems are.
Upvotes: 1