引用相等的性能差异? ((对象)OBJ1 ==(对象)OBJ2)与object.ReferenceEquals(OBJ1,OBJ2) [英] Reference equality performance difference? ((object)obj1 == (object)obj2) vs. object.ReferenceEquals( obj1, obj2 )

查看:261
本文介绍了引用相等的性能差异? ((对象)OBJ1 ==(对象)OBJ2)与object.ReferenceEquals(OBJ1,OBJ2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有使用 object.ReferenceEquals 方法使用的诗句((对象)OBJ1 ==(对象)OBJ2)

在第一种情况下,将有涉及一个静态方法调用,并且在这两种情况下的一些形式浇铸到一个物体的将参与

即使编译平衡了这些方法,不平等呢?结果

 (对象)的obj!= NULL

相比,...

 !object.ReferenceEquals(OBJ,NULL)

我想,在某些时候,会发生一个逻辑否定,无论内!=运算符,或作为施加到所述的ReferenceEquals方法的结果。你觉得呢?

还有可读性考虑的问题。检查的ReferenceEquals时的平等似乎更清晰,但对于不平等,人们可能会错过在 preceding object.ReferenceEquals ,而在 != 在第一个变化是很难忽视。


解决方案

  

时有额外的开销在使用object.ReferenceEquals方法


没有。该方法直接包含最少IL描述进行参考平等检查(备案:这是相当于VB的运营商),并且通常由JIT内联(特别是当针对64位),这样就的开销。

关于可读性:我个人认为, object.ReferenceEquals 可能更为可读的(即使是在否定形式),因为它明确地前presses它的语义。演员阵容到对象可能会产生混淆一些程序员。

我只是发现 讨论这个的文章。它prefers (对象)x ==是因为IL足迹越小。它认为,这可能会促进法 X 使用这种比较的内联。但(没有JIT的任何详细的知识,但在逻辑上和直觉),我认为这是错误的:如果JIT表现什么样优化的C ++编译器,它会考虑之后的方法的内联调用的ReferenceEquals ,所以(内联方法的缘故 X )的内存占用会是完全一样的两种方式。

这就是说:比其他选择一个方式将会对JIT,因此对性能没有任何影响。

Is there extra overhead in using the object.ReferenceEquals method verses using ((object)obj1 == (object)obj2)?

In the first case, there would be a static method call involved, and in both cases some form of casting to an object would be involved.

Even if the compiler balances out those methods, what about inequality?

(object)obj != null

as compared to...

!object.ReferenceEquals(obj,null)

I suppose that at some point, a logical negation would occur, either within the != operator, or as applied to the result of the ReferenceEquals method. What do you think?

There's also the issue of readability to consider. ReferenceEquals seems clearer when checking equality, but for inequality, one might miss the ! preceding object.ReferenceEquals, whereas the != in the first variation is hard to overlook.

解决方案

Is there extra overhead in using the object.ReferenceEquals method

No. The method directly contains the minimal IL description to perform the reference equality check (for the record: it's equivalent to VB's Is operator) and will often be inlined by the JIT (especially when targeting x64) so there's no overhead.

About readability: I personally think that object.ReferenceEquals is potentially more readable (even in the negated form) because it explicitly expresses its semantics. The cast to object may be confusing to some programmers.

I've just found an article discussing this. It prefers (object)x == y because the IL footprint is smaller. It argues that this might facilitate inlining of method X using this comparison. However (without any detailed knowledge of the JIT but logically and intuitively) I believe this is wrong: if the JIT behaves anything like an optimizing C++ compiler, it will consider the method after inlining the call to ReferenceEquals, so (for the sake of inlining method X) the memory footprint will be exactly the same either way.

That is to say: choosing one way over the other will have no impact whatsoever on the JIT and consequently on performance.

这篇关于引用相等的性能差异? ((对象)OBJ1 ==(对象)OBJ2)与object.ReferenceEquals(OBJ1,OBJ2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆