为什么使用GetHash code()以上的equals()? [英] Why use GetHashCode() over Equals()?

查看:185
本文介绍了为什么使用GetHash code()以上的equals()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的HashSet< T>。新增 GetHash code 的结果进行比较。如果这些都是平等的,它会调用等于

HashSet<T>.Add first compares the results of GetHashCode. If those are equal, it calls Equals.

现在,我的理解是为了贯彻 GetHash code 的东西的必须用对象的字段进行。一个简单的例子可以实现在<一个发现href="http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethash$c$c/263416#263416">What是一个覆盖System.Object.GetHash code?。

Now, my understanding is in order to implement GetHashCode, something must be done with the fields of an object. A simple example implementation can be found at What is the best algorithm for an overridden System.Object.GetHashCode?.

在我的测试比较两个上1.000.000对填充有随机数据对象,性能或多或少等于在两者之间。 GetHash code 被实现为在链接例如,等于只需调用等于上的所有字段。那么,为什么总会有需要使用 GetHash code 等于

In my test comparing both on 1.000.000 pairs of objects filled with random data, performance is more or less equal between the two. GetHashCode is implemented as in the linked example, Equals simply calls Equals on all fields. So why would one want to use GetHashCode over Equals ?

推荐答案

对于某些类型的,一个等于测试可能相对昂贵。它通常具有对类的每个字段进行比较。换句话说,它需要线性时间在类的尺寸。更大类比较贵,比较平等。

For some types, an Equals test may be relatively expensive. It typically has to compare every field of the class. In other words, it takes linear time in the size of the class. Bigger classes are more expensive to compare for equality.

现在,如果你需要需要一个对象1000这一别人比较会发生什么?呼叫等于 1000次可能得到昂贵。你需要做的N * 2000个现场访问,如果N类的大小

Now, what happens if you need to need to compare one object against 1000 others? Calling Equals 1000 times might get expensive. You need to do N*2000 field accesses, if N is the size of the class

GetHash code ,而不是生成基于类的内容大多是独特的整数。换句话说,类的字段被访问的一旦的。一旦你有,你就可以比较这整到1000的整数构成其他对象的哈希值codeS。

GetHashCode instead generates a "mostly unique" integer based on the contents of the class. In other words, the class fields are accessed once. And once you have that, you can compare this integer to the 1000 integers that make up the other objects' hash codes.

即使在这样的幼稚使用的情况下,我们现在只需要N *千字段访问

Even in such a naive use case, we now only need N*1000 field accesses.

但是,如果我们的存储的散列code?当我们将对象插入一个哈希集合,它的散列code计算的一次的。现在的任意的时候,我们想要做一个查找的哈希集合,我们只需要计算的一个的散列code(即外部对象),然后你只需要比较简单的整数。 因此n类字段访问(用于新的对象,其散列code我们需要计算),外加一些整数比较,其各不相同,这取决于该算法中,但是:1)相对较少的,和2)廉价。

But what if we store the hash code? When we insert an object into a hash set, its hash code is computed once. Now, any time we want to do a lookup in the hash set, we just need to compute one hash code (that of the external object), and then you just have to compare simple integers. So N class field accesses (for the new object whose hash code we need to compute), plus a number of integer comparisons, which vary, depending on the algorithm, but are 1) relatively few, and 2) cheap.

这篇关于为什么使用GetHash code()以上的equals()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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