Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32319
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") |
| Date | 2015-08-13 08:25 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <mqi29d$n37$1@dont-email.me> (permalink) |
| References | <ebc4e4ca-b5e1-4da2-8583-1a668606cb2d@googlegroups.com> <mq70q5$20v$1@dont-email.me> <5b08503d-429d-429a-b1af-852563619faa@googlegroups.com> <mqa870$9gi$1@dont-email.me> <55cbea08$0$301$14726298@news.sunsite.dk> |
On 8/12/2015 8:51 PM, Arne Vajhøj wrote:
> On 8/10/2015 9:17 AM, Eric Sosman wrote:
>> By the way, is anyone else horrified at the bytecode generated for
>> a switch on a String value? It produces what amounts to *two* switch
>> constructs *and* a set of if ladders (short ones with luck, but ...).
>> Roughly speaking,
>>
>> switch (theString) {
>> case "Foo": fooStuff(); break;
>> case "Bar": barStuff(); break;
>> case "Baz": bazStuff(); break;
>> }
>>
>> turns into something like
>>
>> int _case = 0;
>> switch (theString.hashCode()) {
>> case 70822: if ("Foo".equals(theString)) _case = 1; break;
>> case 66547: if ("Bar".equals(theString)) _case = 2; break;
>> case 66555: if ("Baz".equals(theString)) _case = 3; break;
>> }
>> switch (_case) {
>> case 1: fooStuff(); break;
>> case 2: barStuff(); break;
>> case 3: bazStuff(); break;
>> }
>>
>> String-switching is a convenience, sometimes, but it's a costly one.
>
> Is that code really so bad?
>
> The hash code trick is probably improving performance by reducing
> string comparisons.
Possibly, but possibly not. The hash computation inspects
every character of the string,[*] and the "yes, they're equal"
comparison inspects each character a second time. Unless the
strings share long identical prefixes and differ only in their
suffixes, mismatches between unequal pairs will be detected after
only a few characters are inspected -- or even without inspecting
any characters at all, if the lengths differ. That is, for strings
"chosen at random" (whatever *that* may mean), detecting inequality
a few times might well be cheaper than computing a hash.
[*] Yes, I know that a String object caches its hash value the
first time it's computed and does not recompute it thereafter. I'm
mostly thinking about getting a fresh String from some external source
and switching on it, which involves computing a new hash. If you're
switching on the same String instance over and over again, the hash
cost amortizes toward zero and the trade-off trades off differently.
> The two switches are probably to avoid repeating code in
> case of multiple labels for same action.
Also for fall-through from case to case, especially if the
case labels happen to include different strings with colliding
hash codes.
Don't get me wrong: I've occasionally used string-switch myself
and agree it can be convenient. But it seems to me that one cannot
say "The cost is worth while" if the cost is unknown. (That's part
of what scuppered Boston's bid for the Olympics.) Knowing the cost,
or at least having some notion of it, one may then choose to pay it;
that's all well and good. But paying without knowing? That's not
engineering, that's faith.
And, hey: Isn't this the forum where folks get warned against
indiscriminate use of "+" to concatenate strings? ;-)
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") srcommunicationsvja@gmail.com - 2015-08-08 06:53 -0700
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-08-08 10:39 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eternal Hope <laughingspam99@gmail.com> - 2015-08-09 08:53 +0100
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") e.d.programmer@gmail.com - 2015-08-10 05:36 -0700
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-08-10 09:17 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") e.d.programmer@gmail.com - 2015-08-10 07:56 -0700
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-08-10 11:50 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Arne Vajhøj <arne@vajhoej.dk> - 2015-08-12 20:51 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-08-13 08:25 -0400
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") leif.roar@dimnakorr.com - 2015-08-13 08:43 -0500
Re: PLEASE SOLVE THIS ERROR (case 1:from=="VIJAYAWADA") Eternal Hope <laughingspam99@gmail.com> - 2015-08-10 15:39 +0100
csiph-web