Re: Improving String.equals() implementation



Lew wrote:
Eric Sosman wrote:
/** A trivial class with a "safe" equals() method */
private static class Safe {
private final int value;
private Safe(int value) {
this.value = value;
}

public boolean equals(Object obj) {
return (obj instanceof Safe) && (value == ((Safe)obj).value);
}
}

/** A trivial class with a "fast" equals() method. */
private static class Fast {
private final int value;
private Fast(int value) {
this.value = value;
}

public boolean equals(Object obj) {
return (value == ((Fast)obj).value);
}
}

}

I think I caught the obvious gotchas.

The behaviors of the two 'equals()' implementations are not
equivalent.

A fair benchmark would compare methods with the same behaviors.

I don't understand your objection. The methods are different
because the point of the exercise was to measure the performance
impact of their difference, as a follow-up to Lothar Kimmeringer's
point about the cost of a "redundant" instanceof test. If both
methods had (or both lacked) the test, what would be measured?

--
Eric.Sosman@xxxxxxx
.



Relevant Pages

  • poker odds
    ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
    (comp.lang.java.programmer)
  • problem with vmr9 image compositor - method Draw
    ... I play video with playlist, if i play video about 5x it is ... but so 6round of video playing throw exception int ... private void ConfigureVMR9InWindowlessMode() ... Surface renderTarget = new Surface; ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: using ref keyword performance
    ... // private fields... ... int valueCount = values.Length; ... static void TestWithRefLoop() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The code for "sorting in C sharp considered superior" in five parts for easier download
    ... private bool interchangeSortTest() ... ("Sequence error after interchange sort", ... private bool labelHasFontTag ... int intGridTop, ...
    (comp.programming)
  • Re: resize JTable
    ... >able to easily sort the data, from a user point of view. ... public static final int DESCENDING = -1; ... private Comparator comparator; ... * @param model OrderedTableModel to be kept deduped/sorted. ...
    (comp.lang.java.programmer)