What is the fastest way to check the Equality of strings in .Net?
As per msdn we need to Explicitly use stringcomparision.ordinal to have a increase speed,
but it seems to be wrong in this case.
Without ordinal:
time = Stopwatch.GetTimestamp();
if (str.Equals("Hi"))
{
Console.WriteLine(true);
}
else
{
Console.WriteLine(false);
}
Console.WriteLine("The time taken = {0}", (Stopwatch.GetTimestamp() - time));
//With ordinal:
time = Stopwatch.GetTimestamp();
if (str.Equals("Hi",StringComparison.Ordinal))
{
Console.WriteLine(true);
}
else
{
Console.WriteLine(false);
}
Console.WriteLine("The time taken = {0}", (Stopwatch.GetTimestamp() - time));
The output was : 2934 for (i)
2646 for (ii)
Can anyone explain?