I used this code:
object victim = Guid.Empty;
Guid target = Guid.NewGuid();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000000; i++){
bool equal = ((Guid) victim) == target;
}
Console.WriteLine("Direct cast : {0}", sw.Elapsed);
sw.Reset(); sw.Start();
for (int i = 0; i < 10000000; i++)
{
bool equal = Guid.Equals(victim, target);
}
Console.WriteLine("Guid.Equals : {0}", sw.Elapsed);
sw.Reset(); sw.Start();
string a = victim.ToString(); // as suggested by Mikael
string b = target.ToString();
for (int i = 0; i < 10000000; i++)
{
bool equal = String.Equals(a, b, StringComparison.OrdinalIgnoreCase);
}
Console.WriteLine("String.Equals : {0}", sw.Elapsed);
Console.ReadLine();
And got this result for different values (best scenario):
object victim = Guid.Empty;
Guid target = Guid.NewGuid();
// Direct cast : 00:00:00.1164198
// Guid.Equals : 00:00:02.1268147
// String.Equals : 00:00:00.4129527 // oh my!
And this result for same value (worse scenario)
object victim = Guid.Empty;
Guid target = Guid.Empty;
// Direct cast : 00:00:00.2793173
// Guid.Equals : 00:00:03.5625948
// String.Equals : 00:00:01.7564302
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…