Is it possible to optimize a large switch on strings?

I look at a friend of my library because he was asking about optimization, and I came across a section of code like this:

long digit = 0; switch (word) { case "zero": digit = 0; break; case "a": case "one": digit = 1; break; case "two": digit = 2; break; case "three": digit = 3; break; case "four": digit = 4; break; case "five": digit = 5; break; case "six": digit = 6; break; case "seven": digit = 7; break; case "eight": digit = 8; break; case "nine": digit = 9; break; case "ten": digit = 10; break; case "eleven": digit = 11; break; case "twelve": digit = 12; break; case "thirteen": digit = 13; break; case "fourteen": digit = 14; break; case "fifteen": digit = 15; break; case "sixteen": digit = 16; break; case "seventeen": digit = 17; break; case "eighteen": digit = 18; break; case "nineteen": digit = 19; break; case "twenty": digit = 20; break; case "thirty": digit = 30; break; case "fourty": digit = 40; break; case "fifty": digit = 50; break; case "sixty": digit = 60; break; case "seventy": digit = 70; break; case "eighty": digit = 80; break; case "ninety": digit = 90; break; } return digit; 

I saw here a few questions about how the switch can work , but they usually don't mention string cases. Is there any way to optimize any switch statement like the one above?

+4
source share
6 answers

As Oded said, you can put them in a Dictionary . But in fact, the .NET compiler is already doing this for you: it creates a jump table (via Dictionary<string, SomeDelegate> ) that allows you to include the value in O (1).

However, I do find using Dictionary<string, int> more readable than switch here.

+11
source

You can put them in Dictionary<string,int> and return int for the string key.

 var wordsToNumbers = new Dictionary<string,int>(); wordsToNumbers.Add("one", 1); ... wordsToNumbers.Add("ninety", 90); // elsewhere return wordsToNumbers[word]; 

Note:

As others noted in the comments, the idea is to create a dictionary once and reuse it. One way is to use the field and fill it in the constructor, and then use it in other methods.

+9
source

In this case, a static dictionary might be the best optimization:

 private static readonly Dictionary<string, long> _lookup = new Dictionary<string, long> { { "one", 1 }, { "two", 2 }, { "three", 3 }, // etc... } 

Then just access to use:

 var number = "one"; var result = _lookup[number]; 
+2
source

You can use a dictionary instead of a massive switch. As for timings, switching time between the dictionary. I think the dictionary will be cleaner, but it can do the same.

+1
source
  • Measure if necessary. Use a profiler.

  • The easiest way is a dictionary. It will be very suitable for your use case.

0
source

Inclusion of a line switches to the use of a hash table when the number of rows becomes so large that the hash table is faster than a series of comparisons.

You can check this with Reflector.

0
source

All Articles