快速散列$ C $一个复杂的对象图的Ç [英] Fast HashCode of a Complex Object Graph

查看:154
本文介绍了快速散列$ C $一个复杂的对象图的Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pretty的复杂的对象,我需要得到的唯一这些对象。一种解决方案可以通过覆盖 GetHash code来完成()。我实现了一个code以下注意:

I have a pretty complex object and I need to get uniqueness of these objects. One solution can be done by overriding GetHashCode(). I have implemented a code noted below:

public override int GetHashCode()
{
    return this._complexObject1.GetHashCode() ^
           this._complexObject2.GetHashCode() ^
           this._complexObject3.GetHashCode() ^
           this._complexObject4.GetHashCode() ^
           this._complexObject5.GetHashCode() ^
           this._complexObject6.GetHashCode() ^
           this._complexObject7.GetHashCode() ^
           this._complexObject8.GetHashCode();
}

这些复杂的物体也的重写 GetHash code()并执行的类似的操作

These complex objects also overrides GetHashCode() and does similar operations.

我的项目的要求,这些对象,我处理这些非常的唯一性经常,然后里面还有数据的修改各种方式和地方

My project requires uniqueness of these objects which I deal with these very frequently, and data inside also changes in various ways and places.

我需要一个更快的方法来找到这些复杂的对象,这就需要考虑的唯一的性能内存

提前
谢谢 穆奈姆

Thanks in advance
Munim

推荐答案

考虑您的意见,这听起来像你可能试图依靠GetHash code的自身的确定唯一性。不要那样做。哈希不是的意味着的是独一无二的 - 它的意思是的不可能的两个不相等的对象将返回相同的价值,但并非不可能。如果你想检查一组对象中有没有重复,你会的有无的使用等于为好。

Given your comment, it sounds like you may be trying to rely on GetHashCode on its own to determine uniqueness. Don't do that. Hashes aren't meant to be unique - it's meant to be unlikely that two unequal objects will hash to the same value, but not impossible. If you're trying to check that a set of objects has no duplicates, you will have to use Equals as well.

请注意,使用XOR进行散列code可以使它更容易,你会得到哈希冲突,取决于所涉及的各个散列值。尤其是,它使任何两个相等的字段相互抵消。我一般用这种形式:

Note that using XOR for a hashcode can make it more likely that you'll get hash collisions, depending on the individual hash values involved. In particular, it makes any two equal fields "cancel each other out". I generally use this form:

int hash = 17;
hash = hash * 31 + field1.GetHashCode();
hash = hash * 31 + field2.GetHashCode();
hash = hash * 31 + field3.GetHashCode();
hash = hash * 31 + field4.GetHashCode();
...
return hash;

......但即便如此,这当然不会保证唯一性。您应该使用 GetHash code()来统治的的平等,然后用等于来检查任何可能相等值的事实上的平等。

... but even so, that's certainly not going to guarantee uniqueness. You should use GetHashCode() to rule out equality, and then use Equals to check the actual equality of any potentially equal values.

现在你的问题中提到的速度 - 这听起来像使用一个分析器和一些基准测试的理想场所。你确定这是一个瓶颈?如果你有很多不同类型的所有计算哈希值,你有没有发现其中有这些是问题最大的功臣?

Now your question mentions speed - this sounds like the perfect place to use a profiler and some benchmark tests. Are you sure this is a bottleneck? If you have many different types all computing hash values, have you found out which of these is the biggest contributor to the problem?

一些的优化将取决于你究竟是如何使用这些数据。如果你发现你的很多时间都花在重新计算哈希值,你知道没有改变的值,你可以缓存散列code ...虽然这显然变得棘手时,有其自身引用复杂对象的字段。这是可能的,你可以缓存叶节点的哈希值,特别是在这些叶子节点不经常改变的(但是它们的使用可能会发生变化)。

Some optimisations will depend on exactly how you use the data. If you find that a lot of your time is spent recomputing hashes for values which you know haven't changed, you could cache the hash code... although this obviously becomes trickier when there are fields which themselves refer to complex objects. It's possible that you could cache "leaf node" hashes, particularly if those leaf nodes don't change often (but their usage could vary).

这篇关于快速散列$ C $一个复杂的对象图的Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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