C# int.Parse Optimization

Int keyword

An optimized int.Parse method reduces computational complexity. It uses a for-loop on the characters in a string. It maintains the running total as it proceeds through the characters. It fails on non-trivial number strings.

Note: Faster implementations are possible for parsing ints. Not feature equivalent.

int.Parse optimization

int.Parse("400")     123.07 ns
IntParseFast("400")    2.87 ns

Implementation

To start, we see how you can optimize some kinds of lower-level code such as parsing routines. You can use a simple algorithm to convert an ASCII string to an integer value, in a way that is far faster than int.Parse.

Example unsafe parsing code [C#]

using System;

class Program
{
    public static int IntParseFast(string value)
    {
	int result = 0;
	for (int i = 0; i < value.Length; i++)
	{
	    char letter = value[i];
	    result = 10 * result + (letter - 48);
	}
	return result;
    }

    static void Main()
    {
	const string s = "400";

	// A. Control method from framework.
	int i = int.Parse(s);
	Console.WriteLine(i);

	// B. Control method from framework.
	ushort i2 = ushort.Parse(s);
	Console.WriteLine(i2);

	// C. Use custom method.
	int i3 = IntParseFast(s);
	Console.WriteLine(i3);

	// D. Use custom method.
	ushort i4 = (ushort)IntParseFast(s);
	Console.WriteLine(i4);
    }
}

Output

400
400
400
400
Note

Algorithm description. The method uses a classic C operation to convert a character like '1' to the integer 1. This works because ASCII characters are stored in different positions than the numbers equivalent to them, but in the same order. So shifting the values converts characters to digits. The earlier a digit occurs, the more times it must be multiplied by 10 to correctly parse the number.

Multiplication notes. Here is what happens: As we read the string from the left to the right, the current digit is one tenth of the previous one. We can multiply the previous number by 10 to satisfy this condition. The 48 we use simply shifts ASCII digits to ints.

Performance notes

Question and answer

Why is the custom method faster? Think of how many different things int.Parse must deal with: negative numbers, decimals, null characters, letters, line breaks, spaces, colons and different locales. We don't need all that every time; we just need to parse a series of characters and store it in an int or ushort.

Unsafe code: The first versions of this article used the unsafe context to implement the parsing of the string. In my testing the unsafe code makes this algorithm slower. Therefore, there is really no reason to use unsafe code here. The unsafe implementation has been removed.

Summary

The C# programming language

If you are desperate enough for performance, it is possible to provide alternative implementations for methods such as int.Parse. This can yield significant performance improvements in some programs. You will lose some features of the .NET Framework methods, however.

String Type
.NET