简体   繁体   English

与字符串比较相比,GUID强制转换和比较的成本是多少

[英]How expensive is a GUID cast and comparison vs a string comparison

which would be faster? 哪个会更快?

bool same=(Guid)Identifier==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. 在我的测试中进行直接的UUID-UUID比较VS字符串 - 字符串比较时,UUID比较大约需要1/4的时间作为字符串比较。

However, the casting of String->UUID is expensive. 但是,String-> UUID的转换是昂贵的。 Much more expensive than the UUID->String conversion. 比UUID-> String转换要贵得多。 Both are more expensive than either of the comparison methods. 两者都比任何一种比较方法都贵。

So: If you've got two UUIDs compare the UUIDs directly. 所以:如果你有两个UUID直接比较UUID。 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. 如果您有一个String和一个UUID,请将UUID转换为String并比较字符串。

A Guid == Guid will use code like: Guid == Guid会使用如下代码:

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. 如果不对它进行基准测试,我怀疑Guid会更快,但我们说的是边缘。 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. GUID比较是一个16字节的memcmp。 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. .NET Guid是一个16字节的结构,当表示为字符串时,将以这种模式“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”格式化,大约32个字符。

So represented as a GUID it would take 16 bytes and represented as a string it would take 32*2 = 64 bytes. 因此表示为GUID,它将占用16个字节并表示为字符串,它将占用32 * 2 = 64个字节。

So GUID.Equals() should perform better. 所以GUID.Equals()应该表现得更好。

Also GUID.Equals(GUID) would perform better then guid1 == guid2 because there is no boxing involved in the former. GUID.Equals(GUID)也会比guid1 == guid2表现更好,因为前者没有涉及拳击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM