FANDOM


DatatypeEdit

Using enums that use integer values are faster than string values.

Faster:

   public enum GreekLetters
   {
   	ALPHA= 1,
   	BETA = 2,
   }

Slower:

   public enum GreekLetters
   {
   	ALPHA= "alpha",
   	BETA = "beta",
   }


HasFlagEdit

The code above uses Enum.HasFlag method to check whether PrepareDatabase flag is set, but is astonishingly slow due to boxing operations that are invisilbly generated by C# compiler, even in .NET 4.0.

  if (options.HasFlag(RunOptions.PrepareDatabase)) {
     ...
  }

This is functionally equivalent, and is ~100x faster than the above.

   if ((options & RunOptions.PrepareDatabase) == RunOptions.PrepareDatabase) 

More info at CodeProject.

Inline ValuesEdit

This is slower because we are using the Enum.Parse method, which forces the compiler to go through each item one by one, looking for a match.

   public enum GreekLetters
   {
   	UNKNOWN= 0,
   	ALPHA= 1,
   	BETA = 2,
   	etc...
   
   }
   
   string value = "Alpha";
   GreekLetters letter = (GreekLetters)Enum.Parse( typeof( GreekLetters ), value.ToUpper() );
   
   switch( letter )
   {
      case GreekLetters.ALPHA:
   	  break;
      case GreekLetters.BETA:
   	  break;
      default:
   	  break;
   }

This is faster because the compiler generates a switch-case in the generated bytecode, directly jumping based on the value.

   string value = "Alpha";
   switch ( value.ToUpper() )
   {
   case "ALPHA":
 	  break;
   case "BETA":
 	  break;
   default:
 	  break;
   }

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.