在C#中使用哈希 [英] Using Hash in C#

查看:118
本文介绍了在C#中使用哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多对象。我应该评估一名他们的成员。他们一个接一个。首先是逐一评估它们-------->伪代码

  while(anyObjectExists)
{
Class1 obj = getObject();
double evalNum = eval(obj.member1);
}

但eval是一个耗时的方法。并且很多对象具有相同的成员1。 member1是一个sbyte类型的数组。所以我尝试另一种方式。这是我的方式:------->伪代码

  HashTable评估对象=新的HashTable(); 
while(anyObjectExists)
{
Class1 obj = getObject();
if(evaluateObjects.Contain(obj))
{
double evalNum = evaluateObjects [obj];
}
else
{
double evalNum = eval(obj.member1);
evaluateObjects.Add(obj,evalNum);
}
}

我知道我应该重写getHashCode和Equals方法为sbyte。正如你所看到的,eval方法只使用Class1中的member1。所以我以这种方式添加到我的Class1的方法中

  public override int GetHashCode()
{
返回1;
}
public override bool Equals(Object compareState)
{
if(!this.member1.SequenceEqual(((Class1)compareState).member1))
return假;
返回true;
}

好的。我认为它完成了。但是当我运行我的程序时...这是一个该死的慢程序。它比第一个程序慢很多。我测试了它。它可以找到添加的对象。它没有错。但它非常非常缓慢。我虽然哈希可以检索1或2镜头的数据。我错了什么?



任何帮助都会受到欢迎。

p>您应该返回一个有效的哈希码,如 this.member1.Count()。GetHashCode()不是 1 ,在你的情况下,它总是比较他们的哈希,因为他们是相同的比较他们的平等,你的功能运行速度较慢。 / strong>另外我认为你的 eval 函数比你在sequence equals上的工作更快。实际上你的(Class1)compareState 是耗时的,你的序列比较也很耗时。


I have lots of objects. I should evaluate one of thier members.them one by one. The first is evaluating them one by one --------> pseudo code

while (anyObjectExists)
{
      Class1 obj = getObject();
      double evalNum = eval(obj.member1);
}

but eval is a time consuming method. and lots of objects have same member1. member1 is an array of type sbyte. So I tried to ind another way. That was my way: ------->pseudo code

HashTable evaluatedObject = new HashTable();
while(anyObjectExists)
{
      Class1 obj = getObject();
      if (evaluatedObjects.Contain(obj))
      {
            double evalNum = evaluatedObjects[obj];
      }
      else
      {
            double evalNum = eval(obj.member1);
            evaluatedObjects.Add(obj, evalNum);
      }
}

I knew that I should override getHashCode and Equals method for sbyte. As you can see the eval method only uses member1 from Class1. So I added to methods to my Class1 in this way

    public override int GetHashCode()
    {
        return 1;
    } 
    public override bool Equals(Object compareState)
    {
        if (!this.member1.SequenceEqual(((Class1)compareState).member1))
            return false;
        return true;
    }

Ok. I thought it is done. But when I run my program ... it is a god damn slow program. It is a lot slower than the first program. I tested it. It can find added objects. There is nothing wrong about it. But it is very very slow. I though hash can retrieve data in 1 or 2 shot. What did I wrong?

Any help would be highly welcomed.

解决方案

You should return a valid hash code, like this.member1.Count().GetHashCode() not 1, in your case it always compare them by their hash and because they are a same compare them by their equal, and your function runs slower.

Edit: Also I think your eval function is faster than your work on sequence equals. in fact your (Class1)compareState is time consuming and also your sequence comparison is time consuming too.

这篇关于在C#中使用哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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