Re: Improving String.equals() implementation
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 07 Oct 2008 20:41:57 -0400
Lew wrote:
Eric Sosman wrote:[...]
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?
I didn't say that both methods should have the 'instanceof' test. Duhy.
I said they should have the same behaviors. The first implementation returns 'false' if the argument is a different type. The second one throws an exception if the types differ. The second method should catch the exception and return 'false'.
If one is considering an optimization for a project, one cannot consider "optimizations" that break contract behavior.
Lew, please don't dilute your considerable value with
gratuitous snarky nastiness.
It is true that one of the equals() methods in my code
was faulty, in that it would throw an exception if passed a
reference that was null or that targeted an object of the
wrong type. Yes, I could have embedded the thing in `try'
with a `return false' in the `catch' block. But what good
would that have done?
We all know -- I hope we all know -- that a `try' block
adds precisely nothing to the generated JVM instructions. It
is conceivable that the presence of code in the `catch' or
`finally' might possibly influence the JIT's translation of
bytecodes to opcodes, and my test would have been sharper if
I had chosen to worry about that. Sue me -- better yet, make
the correction and post your own results.
As for coping with actual thrown exceptions, that leads to
the tricky business of determining the likely proportion of
exception-throwing invocations. Such a determination would be
a research project requiring far more effort than I was willing
to spend on idle curiosity about a Usenet post. If your zeal
for knowledge exceeds mine, by all means get out there and do
some studies on the actual frequency of equals() calls with
null or cross-classed arguments. There's surely a scholarly
paper in it for anyone with the stick-to-it-iveness.
I chose the easier road of following Lothar's hypothesis,
which may be paraphrased as "Call equals() with a null or
cross-classed argument? Never! (What, never?) No, never!
(What ne-e-e-ver?) Well, hardly ever!" Quantifying the
difference between "hardly ever" and "never" is a task beyond
my powers -- although it might be within yours -- so I looked
at the limiting question: In the most optimistic case one
could imagine, where "hardly ever" turned out to be exactly
"never," how much gain could one possibly hope for? That is,
I found an upper bound -- a loose bound, no doubt, but a bound
nonetheless. If you can sharpen it, I encourage you to do so.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- References:
- Improving String.equals() implementation
- From: softwarepearls_com
- Re: Improving String.equals() implementation
- From: Lew
- Re: Improving String.equals() implementation
- From: John W Kennedy
- Re: Improving String.equals() implementation
- From: Mark Space
- Re: Improving String.equals() implementation
- From: Eric Sosman
- Re: Improving String.equals() implementation
- From: Mark Space
- Re: Improving String.equals() implementation
- From: Tom Anderson
- Re: Improving String.equals() implementation
- From: Lothar Kimmeringer
- Re: Improving String.equals() implementation
- From: Eric Sosman
- Re: Improving String.equals() implementation
- From: Tom Anderson
- Re: Improving String.equals() implementation
- From: Eric Sosman
- Re: Improving String.equals() implementation
- From: Lew
- Re: Improving String.equals() implementation
- From: Eric Sosman
- Re: Improving String.equals() implementation
- From: Lew
- Improving String.equals() implementation
- Prev by Date: Re: Fast-Fourier transform
- Next by Date: Re: Improving String.equals() implementation
- Previous by thread: Re: Improving String.equals() implementation
- Next by thread: Re: Improving String.equals() implementation
- Index(es):
Relevant Pages
|