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.
evaluatedObjects.Contain(obj)statement that is causing your code to slowdown. All your objects has the same key (the same hash code) what makes your hash table be the same as a linked list. In that case your search will have a complexity of O(N) instead of O(1).