I am trying to make the enclosed subroutine more performant using NET framework 4.6.1 although I will eventually port it to net core 2.2 .
It may run up to 250,000 times when parsing a file.
Using Visual Studio Performance Analyzer I see this routine seems to have a fairly high relative cost in the whole parsing process.
The code is part of a parsing program whose input is a binary file that contains some very old record formats that contain over-signed numbers.
Over-signed Numbers (background)
Instead of a minus sign and in order to save space the last digit is made a letter if the number is negative. Its a very old standard dating back to when computers had limited memory and fixed width records were required for performance.
When parsing I need to convert the last letter back to a number and make the number negative
Some examples of input and output of the routine
00056K = -562
00032N = -325
Current Code (slow)
private int ConvertOverSign(string overSignedString)
{
switch(overSignedString.Substring(overSignedString.Length -1,1))
{
case " ":
return 0;
case "J":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "1");
case "K":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "2");
case "L":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "3");
case "M":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "4");
case "N":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "5");
case "O":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "6");
case "P":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "7");
case "Q":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "8");
case "R":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "9");
case "!":
return -Convert.ToInt32(overSignedString.Substring(0,overSignedString.Length -1) + "0");
default:
return Convert.ToInt32(overSignedString);
}
}