Thursday, November 4, 2010

Dictionary vs. Enum.Parse (.NET C#)

I have often wondered if I should use Enum.Parse or a Dictionary of strings to translation a string to an enumeration value. I have always suspected the Dictionary look up would be faster than the Enum.Parse. I decided it was time to test things out. Here's the sample code:

   1:  enum Place
   2:  {
   3:      Home,
   4:      Work,
   5:      Neither,
   6:      End = Neither
   7:  }
   8:   
   9:  static Dictionary<string, Place> dictionaryPlaces = null; 
  10:   
  11:  static void Main(string[] args)
  12:  {
  13:      const string sHome = "Home";
  14:      const string sWork = "Work";
  15:      const string sNeither = "Neither";
  16:      const string sEnd = "End";
  17:   
  18:      if (!Stopwatch.IsHighResolution) { throw new Exception("No high resolution timer! :("); }
  19:      Stopwatch zStopwatch = Stopwatch.StartNew();           
  20:      dictionaryPlaces = new Dictionary<string, Place>();
  21:      dictionaryPlaces.Add(sHome, Place.Home);
  22:      dictionaryPlaces.Add(sWork, Place.Work);
  23:      dictionaryPlaces.Add(sNeither, Place.Neither);
  24:      dictionaryPlaces.Add(sEnd, Place.End);
  25:      zStopwatch.Stop();
  26:      Console.WriteLine("Dictionary Creation: " + zStopwatch.ElapsedTicks);
  27:   
  28:      for (int nIdx = 0; nIdx < 2; nIdx++)
  29:      {
  30:      Console.WriteLine(Environment.NewLine + "--Dictionary Lookup");
  31:      LookupEnumByDictionary(sHome);
  32:      LookupEnumByDictionary(sWork);
  33:      LookupEnumByDictionary(sNeither);
  34:      LookupEnumByDictionary(sEnd);
  35:   
  36:      Console.WriteLine(Environment.NewLine + "--Enum Lookup");
  37:      LookupEnum(sHome);
  38:      LookupEnum(sWork);
  39:      LookupEnum(sNeither);
  40:      LookupEnum(sEnd);
  41:      }
  42:      Console.ReadLine();
  43:  }
  44:   
  45:  static void LookupEnumByDictionary(string sValue)
  46:  {
  47:      Stopwatch zStopwatch = Stopwatch.StartNew();
  48:      Place ePlace = dictionaryPlaces[sValue];
  49:      zStopwatch.Stop();
  50:      Console.WriteLine(ePlace.ToString() + "(" + zStopwatch.ElapsedTicks + ")");
  51:  }
  52:   
  53:  static void LookupEnum(string sValue)
  54:  {
  55:      Stopwatch zStopwatch = Stopwatch.StartNew();
  56:      Place ePlace = (Place)Enum.Parse(typeof(Place), sValue);
  57:      zStopwatch.Stop();
  58:      Console.WriteLine(ePlace.ToString() + "(" + zStopwatch.ElapsedTicks + ")");
  59:  }

Here's the output:


Dictionary Creation: 15315
 
--Dictionary Lookup
Home(4218)
Work(10)
Neither(2)
Neither(1)
 
--Enum Lookup
Home(10)
Work(5)
Neither(5)
Neither(4)
 
--Dictionary Lookup
Home(2)
Work(1)
Neither(1)
Neither(1)
 
--Enum Lookup
Home(5)
Work(5)
Neither(4)
Neither(5)

The initial hits of the Dictionary construction and initial query are large but if you have a program performing a lot of string to enumeration values the Dictionary would result in the best performance over time. I added the second pass through the loop as I guessed it might further reveal the likely pattern.

No comments:

Post a Comment