From https://github.com/dotnet/corefx/issues/594
When parsing strings to enums with TryParse, in some cases this takes way longer than in all the other cases. After a bit of research I found that all the strings with a digit as first char are slower. This is due to the attempt to parse the string to the underlying type (https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Enum.cs#L396) with a Convert.ChangeType, which can throw an exception, because further down the line it uses (for example) Int32.Parse instead of TryParse (https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Convert.cs#L1118).
The time it takes to create the Exception is significant. In my project I ended up writing a new Enum parser to circumvent this issue.
"Something": 240.49 ns per parse
"1Somethin": 37178.57 ns per parse
"123456789": 485.63 ns per parse
As users expect Convert.ToXxx() to throw an exception when it fails, changing this is a bad idea I think. Possible solutions I can think of:
From https://github.com/dotnet/corefx/issues/594
When parsing strings to enums with TryParse, in some cases this takes way longer than in all the other cases. After a bit of research I found that all the strings with a digit as first char are slower. This is due to the attempt to parse the string to the underlying type (https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Enum.cs#L396) with a Convert.ChangeType, which can throw an exception, because further down the line it uses (for example) Int32.Parse instead of TryParse (https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Convert.cs#L1118).
The time it takes to create the Exception is significant. In my project I ended up writing a new Enum parser to circumvent this issue.
"Something": 240.49 ns per parse
"1Somethin": 37178.57 ns per parse
"123456789": 485.63 ns per parse
As users expect Convert.ToXxx() to throw an exception when it fails, changing this is a bad idea I think. Possible solutions I can think of: