I am trying to optimize a method.
Here is the original method:
private const string Separator = "::";
private char[] SeparatorChars = Separator.ToCharArray();
public string GetSubName(string name)
{
if (name.Contains(Separator))
{
return name.Split(SeparatorChars)[0];
}
return "INVALID";
}
if name doesn't contain ::, return INVALID else return the first element in the array by generated by Split with ::.
I have wrote the following optimized method:
public string GetSubNameOpt(string name)
{
var index = name.IndexOf(Separator, StringComparison.Ordinal);
if (index >= 0)
{
return name.AsSpan().Slice(0, index).ToString();
}
return "INVALID";
}
The goal was to omit the second O(N) iteration over the string in order to split it in case it contains the :: substring.
I benchmarked both method using BenchmarkDotNet and here are the results

Now.. while, as expected, the Optimized method is better in time and memory in the "name contains :: case" due usage of AsSpan and removing of 1 O(N) iteration of the string, it was surprise to me that the Non Optimized method was better for the INVALID case.
EDIT
Running with a non empty, not containing :: case.

Again, the Optimized method is slower...
Can you explain what is causing this behavior?