Which is faster? This:
哪个更快?这个:
bool isEqual = (MyObject1 is MyObject2)
Or this:
bool isEqual = ("blah" == "blah1")
It would be helpful to figure out which one is faster. Obviously, if you apply .ToUpper() to each side of the string comparison like programmers often do, that would require reallocating memory which affects performance. But how about if .ToUpper() is out of the equation like in the above sample?
找出哪一个更快会有所帮助。显然,如果你将.ToUpper()应用于字符串比较的每一边,就像程序员经常做的那样,那就需要重新分配影响性能的内存。但是,如果.ToUpper()不像上面的样本那样,怎么样?
7 个解决方案
#1
9
I'm a little confused here.
我在这里有点困惑。
As other answers have noted, you're comparing apples and oranges. ::rimshot::
正如其他答案所指出的那样,你要比较苹果和橘子。 ::边敲击::
If you want to determine if an object is of a certain type use the is
operator.
如果要确定某个对象是否属于某种类型,请使用is运算符。
If you want to compare strings use the ==
operator (or other appropriate comparison method if you need something fancy like case-insensitive comparisons).
如果你想比较字符串,可以使用==运算符(或其他适当的比较方法,如果你需要像对不区分大小写的比较那样花哨的东西)。
How fast one operation is compared to the other (no pun intended) doesn't seem to really matter.
一个操作与另一个操作相比的速度(没有双关语)似乎并不重要。
After closer reading, I think that you want to compare the speed of string comparisions with the speed of reference comparisons (the type of comparison used in the System.Object base type).
仔细阅读之后,我认为您希望将字符串比较的速度与参考比较的速度(System.Object基类型中使用的比较类型)进行比较。
If that's the case, then the answer is that reference comparisons will never be slower than any other string comparison. Reference comparison in .NET is pretty much analogous to comparing pointers in C - about as fast as you can get.
如果是这种情况,那么答案是参考比较永远不会比任何其他字符串比较慢。 .NET中的参考比较非常类似于比较C中的指针 - 尽可能快。
However, how would you feel if a string variable s
had the value "I'm a string"
, but the following comparison failed:
但是,如果字符串变量s的值为“我是一个字符串”,您会感觉如何,但以下比较失败:
if (((object) s) == ((object) "I'm a string")) { ... }
If you simply compared references, that might happen depending on how the value of s
was created. If it ended up not being interned, it would not have the same reference as the literal string, so the comparison would fail. So you might have a faster comparison that didn't always work. That seems to be a bad optimization.
如果您只是比较引用,则可能会发生这种情况,具体取决于s的值的创建方式。如果它最终没有被实现,它将没有与文字字符串相同的引用,因此比较将失败。所以你可能有一个更快的比较,并不总是有效。这似乎是一个糟糕的优化。
#2
5
According to the book Maximizing .NET Performance the call
根据“最大化.NET性能”一书的说法
bool isEqual = String.Equals("test", "test");
is identical in performance to
性能相同
bool isEqual = ("test" == "test");
The call
bool isEqual = "test".Equals("test");
is theoretically slower than the call to the static String.Equals method, but I think you'll need to compare several million strings in order to actually detect a speed difference.
理论上比调用静态String.Equals方法慢,但我认为你需要比较几百万个字符串才能真正检测到速度差异。
My tip to you is this; don't worry about which string comparison method is slower or faster. In a normal application you'll never ever notice the difference. You should use the way which you think is most readable.
我给你的小费是这个;不要担心哪种字符串比较方法更慢或更快。在正常的应用程序中,您永远不会注意到差异。您应该使用您认为最具可读性的方式。
#3
4
The first one is used to compare types not values. If you want to compare strings with a non-sensitive case you can use:
第一个用于比较类型而不是值。如果要将字符串与非敏感案例进行比较,可以使用:
string toto = "toto";
string tata = "tata";
bool isEqual = string.Compare(toto, tata, StringComparison.InvariantCultureIgnoreCase) == 0;
Console.WriteLine(isEqual);
#4
1
How about you tell me? :)
你告诉我怎么样? :)
Take the code from this Coding Horror post, and insert your code to test in place of his algorithm.
从这个Coding Horror帖子中获取代码,并插入代码进行测试以代替他的算法。
#5
0
Comparing strings with a "==" operator compares the contents of the string vs. the string object reference. Comparing objects will call the "Equals" method of the object to determine whether they are equal or not. The default implementation of Equals is to do a reference comparison, returning True if both object references are the same physical object. This will likely be faster than the string comparison, but is dependent on the type of object being compared.
使用“==”运算符比较字符串会比较字符串的内容与字符串对象引用。比较对象将调用对象的“等于”方法以确定它们是否相等。 Equals的默认实现是进行引用比较,如果两个对象引用都是同一个物理对象,则返回True。这可能比字符串比较更快,但取决于要比较的对象的类型。
#6
0
I'd assume that comparing the objects in your first example is going to be about as fast as it gets since its simply checking if both objects point to the same address in memory.
我假设比较第一个例子中的对象的速度和它一样快,因为它只是检查两个对象是否指向内存中的相同地址。
As it has been mentioned several times already, it is possible to compare addresses on strings as well, but this won't necessarily work if the two strings are allocated from different sources.
正如已经多次提到的那样,也可以比较字符串上的地址,但如果两个字符串是从不同的源分配的话,这不一定有效。
Lastly, its usually good form to try and compare objects based on type whenever possible. Its typically the most concrete method of identification. If your objects need to be represented by something other than their address in memory, its possible to use other attributes as identifiers.
最后,它通常很好的形式,尽可能尝试和基于类型比较对象。它通常是最具体的识别方法。如果您的对象需要由内存中的地址以外的其他内容表示,则可以使用其他属性作为标识符。
#7
0
If I understand the question and you really want to compare reference equality with the plain old "compare the contents": Build a testcase and call object.ReferenceEquals compared against a == b.
如果我理解了这个问题,你真的想要将引用相等性与普通的“比较内容”进行比较:构建一个测试用例并调用object.ReferenceEquals与a == b进行比较。
Note: You have to understand what the difference is and that you probably cannot use a reference comparison in most scenarios. If you are sure that this is what you want it might be a tiny bit faster. You have to try it yourself and evaluate if this is worth the trouble at all..
注意:您必须了解区别是什么,并且在大多数情况下您可能无法使用参考比较。如果你确定这是你想要的,它可能会快一点。你必须自己尝试并评估这是否值得麻烦...
#1
9
I'm a little confused here.
我在这里有点困惑。
As other answers have noted, you're comparing apples and oranges. ::rimshot::
正如其他答案所指出的那样,你要比较苹果和橘子。 ::边敲击::
If you want to determine if an object is of a certain type use the is
operator.
如果要确定某个对象是否属于某种类型,请使用is运算符。
If you want to compare strings use the ==
operator (or other appropriate comparison method if you need something fancy like case-insensitive comparisons).
如果你想比较字符串,可以使用==运算符(或其他适当的比较方法,如果你需要像对不区分大小写的比较那样花哨的东西)。
How fast one operation is compared to the other (no pun intended) doesn't seem to really matter.
一个操作与另一个操作相比的速度(没有双关语)似乎并不重要。
After closer reading, I think that you want to compare the speed of string comparisions with the speed of reference comparisons (the type of comparison used in the System.Object base type).
仔细阅读之后,我认为您希望将字符串比较的速度与参考比较的速度(System.Object基类型中使用的比较类型)进行比较。
If that's the case, then the answer is that reference comparisons will never be slower than any other string comparison. Reference comparison in .NET is pretty much analogous to comparing pointers in C - about as fast as you can get.
如果是这种情况,那么答案是参考比较永远不会比任何其他字符串比较慢。 .NET中的参考比较非常类似于比较C中的指针 - 尽可能快。
However, how would you feel if a string variable s
had the value "I'm a string"
, but the following comparison failed:
但是,如果字符串变量s的值为“我是一个字符串”,您会感觉如何,但以下比较失败:
if (((object) s) == ((object) "I'm a string")) { ... }
If you simply compared references, that might happen depending on how the value of s
was created. If it ended up not being interned, it would not have the same reference as the literal string, so the comparison would fail. So you might have a faster comparison that didn't always work. That seems to be a bad optimization.
如果您只是比较引用,则可能会发生这种情况,具体取决于s的值的创建方式。如果它最终没有被实现,它将没有与文字字符串相同的引用,因此比较将失败。所以你可能有一个更快的比较,并不总是有效。这似乎是一个糟糕的优化。
#2
5
According to the book Maximizing .NET Performance the call
根据“最大化.NET性能”一书的说法
bool isEqual = String.Equals("test", "test");
is identical in performance to
性能相同
bool isEqual = ("test" == "test");
The call
bool isEqual = "test".Equals("test");
is theoretically slower than the call to the static String.Equals method, but I think you'll need to compare several million strings in order to actually detect a speed difference.
理论上比调用静态String.Equals方法慢,但我认为你需要比较几百万个字符串才能真正检测到速度差异。
My tip to you is this; don't worry about which string comparison method is slower or faster. In a normal application you'll never ever notice the difference. You should use the way which you think is most readable.
我给你的小费是这个;不要担心哪种字符串比较方法更慢或更快。在正常的应用程序中,您永远不会注意到差异。您应该使用您认为最具可读性的方式。
#3
4
The first one is used to compare types not values. If you want to compare strings with a non-sensitive case you can use:
第一个用于比较类型而不是值。如果要将字符串与非敏感案例进行比较,可以使用:
string toto = "toto";
string tata = "tata";
bool isEqual = string.Compare(toto, tata, StringComparison.InvariantCultureIgnoreCase) == 0;
Console.WriteLine(isEqual);
#4
1
How about you tell me? :)
你告诉我怎么样? :)
Take the code from this Coding Horror post, and insert your code to test in place of his algorithm.
从这个Coding Horror帖子中获取代码,并插入代码进行测试以代替他的算法。
#5
0
Comparing strings with a "==" operator compares the contents of the string vs. the string object reference. Comparing objects will call the "Equals" method of the object to determine whether they are equal or not. The default implementation of Equals is to do a reference comparison, returning True if both object references are the same physical object. This will likely be faster than the string comparison, but is dependent on the type of object being compared.
使用“==”运算符比较字符串会比较字符串的内容与字符串对象引用。比较对象将调用对象的“等于”方法以确定它们是否相等。 Equals的默认实现是进行引用比较,如果两个对象引用都是同一个物理对象,则返回True。这可能比字符串比较更快,但取决于要比较的对象的类型。
#6
0
I'd assume that comparing the objects in your first example is going to be about as fast as it gets since its simply checking if both objects point to the same address in memory.
我假设比较第一个例子中的对象的速度和它一样快,因为它只是检查两个对象是否指向内存中的相同地址。
As it has been mentioned several times already, it is possible to compare addresses on strings as well, but this won't necessarily work if the two strings are allocated from different sources.
正如已经多次提到的那样,也可以比较字符串上的地址,但如果两个字符串是从不同的源分配的话,这不一定有效。
Lastly, its usually good form to try and compare objects based on type whenever possible. Its typically the most concrete method of identification. If your objects need to be represented by something other than their address in memory, its possible to use other attributes as identifiers.
最后,它通常很好的形式,尽可能尝试和基于类型比较对象。它通常是最具体的识别方法。如果您的对象需要由内存中的地址以外的其他内容表示,则可以使用其他属性作为标识符。
#7
0
If I understand the question and you really want to compare reference equality with the plain old "compare the contents": Build a testcase and call object.ReferenceEquals compared against a == b.
如果我理解了这个问题,你真的想要将引用相等性与普通的“比较内容”进行比较:构建一个测试用例并调用object.ReferenceEquals与a == b进行比较。
Note: You have to understand what the difference is and that you probably cannot use a reference comparison in most scenarios. If you are sure that this is what you want it might be a tiny bit faster. You have to try it yourself and evaluate if this is worth the trouble at all..
注意:您必须了解区别是什么,并且在大多数情况下您可能无法使用参考比较。如果你确定这是你想要的,它可能会快一点。你必须自己尝试并评估这是否值得麻烦...