
Often when processing text in your C# programs, you need to split a line in two parts based on a character or substring. Although the Split method works fine for this, with large amounts of data a faster solution is available.
We introduce two methods here, UseSplit and UseIndexOfSubstring. Both of the methods do the same thing on valid input: they split the string based on the delimiter character. In the Main method, then, you can see the data string which contains one comma. We finally execute these two methods a whole bunch of times to determine which is faster.
This C# example program shows one way to optimize Split method calls. Substring is used.
Program that tests split optimization [C#]
using System;
using System.Diagnostics;
class Program
{
static void UseSplit(string data, char[] separator)
{
string[] parts = data.Split(separator);
string part1 = parts[0];
string part2 = parts[1];
}
static void UseIndexOfSubstring(string data, char separator)
{
int comma = data.IndexOf(separator);
string part1 = data.Substring(0, comma);
string part2 = data.Substring(comma + 1);
}
const int _max = 1000000;
static void Main()
{
string data = "Dot Net Perls,Something Else";
char[] separator = { ',' };
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
UseSplit(data, separator);
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
UseIndexOfSubstring(data, ',');
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.Read();
}
}
Output
201.70 ns
65.82 nsResult. You can see that using the Split method in this situation required about three times longer than the custom method. Using IndexOf (or an equivalent loop) and the Substring method thus serves as an optimization in this situation. In places where more than one delimiter appears, a more complex custom method would likely be faster as well. This is partly because no array will need to be allocated; you can store the strings in local variables instead.

In my programs, I call the Split method constantly, but in some places a simple custom implementation is a better choice. In programs that process lots of data, custom implementations such as the UseIndexOfSubstring method can be advantageous, particularly as they also reduce allocations.
String Type