Am 26.02.2010 13:58, schrieb Alan Bateman:
Jason Mehrens wrote:
Alan,

Shouldn't the loading of 'this.count' into 'len' be only performed if 'h' is zero? Otherwise, when hash is not zero we perform a little unnecessary work every time hashCode is called.

Jason
That's a good suggestion although it might be hard to measure any difference.


In case of HotSpot compiled code, I guess, len will not be filled by this.count before it is actually needed, but in interpreted code Jason may be right.

So optimum could be:

public int hashCode() {
  int h = hash;
  if (h == 0) {
    int len = count;
    if (len>  0) {
      char[] val = value;
      for (int i = 0, off = offset; i<  len; i++)
        h = 31*h + val[off++];
      hash = h;
    }
  }
  return h;
}


- Ulf

Reply via email to