A friend of mine commented on my last post asking about how much faster the static string.Equals method is than the instance string.Equals method. To satiate both of our curiosities, I have created this benchmarking application:
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
string a = "hello";
string b = "hi";
stopwatch.Start();
for (int i = 0; i < 10000000; i++)
{
a.Equals(b);
}
stopwatch.Stop();
Console.WriteLine("Instance string.Equals over 10,000,000 iterations: "
+ stopwatch.ElapsedMilliseconds + " ms");
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < 10000000; i++)
{
string.Equals(a, b);
}
stopwatch.Stop();
Console.WriteLine("Static string.Equals over 10,000,000 iterations: "
+ stopwatch.ElapsedMilliseconds + " ms");
Console.ReadKey();
}
The results of 5 runs, where “I” is the instance method and “S” is the static method, and the times are in milliseconds: