after a bit speedtracing I found a piece of code (called very very often) which converts values of one enum to values of another enum, like this:
public Enum2 ConvertToEnum2(Enum1 enum1) { switch(enum1) { case Enum1.One: return Enum2.One; break; case Enum1.Two: return Enum2.Two; break; } }
Would it me more performant if I save those conversions in a Dictionary and just do something like this:
public Enum2 ConvertToEnum2(Enum1 enum1) { return m_ConversionTable[enum1]; }
Thanks for your comments!
A dictionary definitely would not be faster.
If the enums in the Enum1 are sequential then an array of Enum2 would likely be faster (but that could be marginal). IF the Enum1 is close to sequential so that the array isn’t too spares it may still be useful approach.
For an enum with the [Flags] attribute then the switch is probably the best.