开发者

How expensive is a GUID cast and comparison vs a string comparison

开发者 https://www.devze.com 2022-12-18 01:02 出处:网络
which would be faster? bool same=(Guid)Identifi开发者_StackOverflower==id; bool same=String.Equals(string1,string2, StringComparison.OrdinalIgnoreCase);

which would be faster?

bool same=(Guid)Identifi开发者_StackOverflower==id;

bool same=String.Equals(string1,string2, StringComparison.OrdinalIgnoreCase);


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


In my testing doing a straight UUID-UUID comparison VS String-String comparison, the UUID comparison takes about 1/4 the time as the String comparison.

However, the casting of String->UUID is expensive. Much more expensive than the UUID->String conversion. Both are more expensive than either of the comparison methods.

So: If you've got two UUIDs compare the UUIDs directly. If you've got two Strings compare the Strings directly. If you've got one String and one UUID, convert the UUID to a String and compare the Strings.


A Guid == Guid will use code like:

public bool Equals(Guid g)
{
if (g._a != this._a)
{
    return false;
}
if (g._b != this._b)
{
    return false;
}

while the string compare in your example will use an unsafe pointer comparison.

Without benchmarking it, I suspect the Guid will be faster, but we're talking marginal. And you really need to crank up the number of comparisons to the multi millions for it to matter.

Both comparisons will break out early, meaning left to right, so that will also impact the speed. The string compare has more checks before the comparison occurs and one more method call as well.


A GUID compare is a memcmp of 16 bytes. It isn't going to be worse worse than a string compare, but if you care about performance that much you shouldn't be using managed code.


.NET Guid is a 16 byte structure which when represented as a string will be formatted in this pattern "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" which is about 32 characters.

So represented as a GUID it would take 16 bytes and represented as a string it would take 32*2 = 64 bytes.

So GUID.Equals() should perform better.

Also GUID.Equals(GUID) would perform better then guid1 == guid2 because there is no boxing involved in the former.

0

精彩评论

暂无评论...
验证码 换一张
取 消