Strangely, if the hashCode() method in the Java version is replaced with one generated by Eclipse, the java program runs much slower.
In fact, the execution time is more than doubled.
Why is this so?
The Eclipse generated version of hashCode():
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + goats;
result = prime * result + lions;
result = prime * result + wolves;
return result;
}
The version in the original code:
@Override
public int hashCode() {
final int magic = 0x9e3779b9;
int seed = 0;
seed ^= this.goats + magic + (seed > 2);
seed ^= this.lions + magic + (seed > 2);
seed ^= this.wolves + magic + (seed > 2);
return seed;
}
The two HashCode() methods both have about the same execution time.
Example:
Forest.makeForest(517, 555, 506)
With original hashCode(): 8.177 s
With Eclipse generated hashCode(): 19.237 s
(100% repeatable with only a few 100ms diff between executions)