Dictionary <string, T> (StringComparer) and dictionary <string, T> and key storage. ToUpper: Which one do you prefer?

Disclaimer: there may be micro-YAGNI optimization, but listen to me.

The problem is the case-insensitive implementation of the table.

  • My old way: while filling out the dictionary, the uppercase key is before insertion. Uppercase key when someone gives you a key to search.
  • New way (I found out today): The dictionary accepts an IComparer implementation, so I can go through StringComparer.InvariantCultureIgnoreCase . I think he would delegate String.Compare (x, y, SomeIgnoreCaseEnum)

The new method has the advantage that I do not need to guarantee the execution of .ToUpper () in each of the n places where the dictionary is searched.

My question is: which one is more efficient? Just curious, I think ...

Update: Note. I do not need to know the source key that was inserted. Also used keys are agnostics of culture.

+4
source share
4 answers

It is possible that the upper case will be more efficient, since then it can make an ordinal comparison ... but I very much doubt that this is a bottleneck for you. As always, profile before making code changes based on performance.

Ultimately, indicating string comparison:

  • means you don't have to be as careful as you use the dictionary
  • means that the original enclosure is supported in a key that can help diagnose in some cases
  • Explicitly states how you want the keys to be processed. You end up declaring this only once, at the time of creation - which leads to a clearer IMO code.
+3
source

Check the entry . It is still valid today.

Excerpt from MSDN: New Guidelines for Using Strings in Microsoft.NET 2.0

+1
source

I do not know about performance, but I prefer the StringComparer parameter. With ToUpper, you lose information (source case). True, you may not need this, but one day you may not want to work anymore to save it (thus, it is safe from the YAGNI principle).

I would also once forget to call ToUpper and find myself in a world of pain. But my unit tests naturally saved me.

+1
source

I would accept the Oded 'Go profile!' comment :) which was the main obvious suggestion.

From my test ( source code here )

To search for 1M ContainsKey,

  • ToUpper Approach: 1123 ms
  • Dictionary (OrdinalIgnoreCaseComparer): 971 ms

I find a dictionary with an injected Comparer parameter to work better and less hassle. So for me there is no ToUpper () anymore.

0
source

Source: https://habr.com/ru/post/1307633/


All Articles