简体   繁体   English

为什么ReferenceEquals()比==更快

[英]Why ReferenceEquals() is faster than ==

I run the following code 10000 times(a and b are both instances of class C) 我运行以下代码10000次(a和b都是类C的实例)

1.ReferenceEquals(a,b)  0.1 ms
2.a==b                  0.7 ms
3.a.Equals(b)           0.8 ms

Could you tell me why 1 is faster than 2 and 3... 你能告诉我为什么1比2和3快吗...

It's hard to say without seeing any code, but: 不看任何代码就很难说,但是:

This is probably because the class in question has implemented the == operator, so that it doesn't compare references. 这可能是因为所讨论的类已实现==运算符,因此它不会比较引用。 Instead it likely is comparing the contents of the object in some way, which will of course be substantially slower. 相反,它可能是以某种方式比较对象的内容,这当然会慢得多。

(The string class does this, for example.) (例如, string类执行此操作。)

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

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