Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #15017 > unrolled thread
| Started by | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| First post | 2012-06-03 11:40 -0700 |
| Last post | 2012-06-06 00:15 -0700 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
case strings Roedy Green <see_website@mindprod.com.invalid> - 2012-06-03 11:40 -0700
Re: case strings Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-03 13:37 -0700
Re: case strings markspace <-@.> - 2012-06-03 19:19 -0700
Re: case strings Roedy Green <see_website@mindprod.com.invalid> - 2012-06-04 07:53 -0700
Re: case strings Lew <noone@lewscanon.com> - 2012-06-05 23:53 -0700
Re: case strings Roedy Green <see_website@mindprod.com.invalid> - 2012-06-04 07:57 -0700
Re: case strings "Mike Schilling" <mscottschilling@hotmail.com> - 2012-06-04 08:18 -0700
Re: case strings Joshua Cranmer <Pidgeot18@verizon.invalid> - 2012-06-04 11:30 -0400
Re: case strings Lew <noone@lewscanon.com> - 2012-06-06 00:15 -0700
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-06-03 11:40 -0700 |
| Subject | case strings |
| Message-ID | <robns7d4t1tn9g0q8i1g0rph1g36cnk7ko@4ax.com> |
has anyone benchmarked or decompiled to see how the new case string labels are compiled. Are they faster than setting up a HashMap to classify the strings? In theory they could be since the literal values are known at compile time, where HashMap does not have that advantage. -- Roedy Green Canadian Mind Products http://mindprod.com Controlling complexity is the essence of computer programming. ~ Brian W. Kernighan 1942-01-01 .
[toc] | [next] | [standalone]
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Date | 2012-06-03 13:37 -0700 |
| Message-ID | <YVPyr.3287$At.402@newsfe23.iad> |
| In reply to | #15017 |
On 6/3/12 11:40 AM, Roedy Green wrote: > has anyone benchmarked or decompiled to see how the new case string > labels are compiled. Are they faster than setting up a HashMap to > classify the strings? In theory they could be since the literal > values are known at compile time, where HashMap does not have that > advantage. > It took me a second to realize you meant switch/case with Strings. I thought you were talking about letter case and case sensitivity, and the rest became a non sequitur. I haven't looked into the mechanisms specifically, but I would imaging there could be significant optimizations that could be done at compile time (java->bytecode or bytecode->native), depending on size of the switch statement. On the other hand, a Map has other advantages (like being able to be dynamically defined). Use what ever is clearest for your use-case, then if, and only if, it isn't fast enough (as indicated by profiling tools), optimize it.
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-06-03 19:19 -0700 |
| Message-ID | <jqh5us$n1v$1@dont-email.me> |
| In reply to | #15020 |
On 6/3/2012 1:37 PM, Daniel Pitts wrote: > It took me a second to realize you meant switch/case with Strings. I > thought you were talking about letter case and case sensitivity, and the > rest became a non sequitur. > > I haven't looked into the mechanisms specifically, but I would imaging Thanks for translating from Roedyese into English. I was having no luck at all with it. Iirc, the strings in a case statement are hashed. I seem to recall the implementer posting about how it worked, and he used a perfect hash for the strings, so it should be faster (slightly) than a regular hash map.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-06-04 07:53 -0700 |
| Message-ID | <0mips7l4ste07c530h6lqkvvk5ie5rie98@4ax.com> |
| In reply to | #15020 |
On Sun, 03 Jun 2012 13:37:13 -0700, Daniel Pitts <newsgroup.nospam@virtualinfinity.net> wrote, quoted or indirectly quoted someone who said : > >On the other hand, a Map has other advantages (like being able to be >dynamically defined). Use what ever is clearest for your use-case, then >if, and only if, it isn't fast enough (as indicated by profiling tools), >optimize it. The primary motive for such questions is curiosity. Secondarily I want to know which to use by default when there is no pressing high level need for either. I got immense joy looking as some of the assembler code that Jet generated, realising is many cases it was faster than I would ever have the patience to create, taking pipelines into consideration. If I were to learn string labels were handled it some clever way it would give me great pleasure. It would be sort of like everyone being issued a Ferrari powered electrically. -- Roedy Green Canadian Mind Products http://mindprod.com Controlling complexity is the essence of computer programming. ~ Brian W. Kernighan 1942-01-01 .
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-06-05 23:53 -0700 |
| Message-ID | <jqmuo8$il0$1@news.albasani.net> |
| In reply to | #15041 |
Roedy Green wrote: > If I were to learn string labels were handled it some clever way it > would give me great pleasure. Joseph D. Darcy's Oracle Weblog <https://blogs.oracle.com/darcy/entry/project_coin_string_switch_anatomy> "... com.sun.tools.javac.comp.Lower, the compiler phase which translates away syntactic sugar, lowering structures to trees implementing the formerly sugar-coated functionality. For example, Lower already had a method to translate enum switches into a switch on an integer value retrieved from an enum → int map. The initial strings in switch implementation uses a similar technique: a single string switch in source code is lowered into a series of two switches. The first switch is a new synthesized switch on the string's hash code, which gets mapped to a label's ordinal position on the list of case statements. The second switch is structurally identical to the original string switch from the source except that the string case labels are replaced by integer positions and the computed position from the synthesized switch is the expression being switched on." and about the compliance tests: "The new tests verify that the proper structural checks are enforced for string switches as well as verify that the proper execution paths are taken on different inputs for switches with a variety of control flow shapes, including multiple case labels, case labels with colliding hash codes, and nested switches." N.b., this article links to <http://blogs.sun.com/darcy/entry/how_to_cross_compile_for> "... make sure to set the bootclasspath!" Thanks for the chance to flex my google-fu. > It would be sort of like everyone being issued a Ferrari powered > electrically. That would be a Tesla. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-06-04 07:57 -0700 |
| Message-ID | <o2jps7t44ui9adpocgptdsrun43qgaalus@4ax.com> |
| In reply to | #15017 |
On Sun, 03 Jun 2012 11:40:03 -0700, Roedy Green <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted someone who said : >has anyone benchmarked or decompiled to see how the new case string >labels are compiled. I gather this was the confusing sentence. What should I have called the feature? What the desperation of trying to deduce how it worked from benchmark timings what threw you off? -- Roedy Green Canadian Mind Products http://mindprod.com Controlling complexity is the essence of computer programming. ~ Brian W. Kernighan 1942-01-01 .
[toc] | [prev] | [next] | [standalone]
| From | "Mike Schilling" <mscottschilling@hotmail.com> |
|---|---|
| Date | 2012-06-04 08:18 -0700 |
| Message-ID | <jqijl8$sah$1@dont-email.me> |
| In reply to | #15042 |
"Roedy Green" <see_website@mindprod.com.invalid> wrote in message news:o2jps7t44ui9adpocgptdsrun43qgaalus@4ax.com... > On Sun, 03 Jun 2012 11:40:03 -0700, Roedy Green > <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted > someone who said : > >>has anyone benchmarked or decompiled to see how the new case string >>labels are compiled. > > I gather this was the confusing sentence. What should I have called > the feature? Say, string-valued case labels.
[toc] | [prev] | [next] | [standalone]
| From | Joshua Cranmer <Pidgeot18@verizon.invalid> |
|---|---|
| Date | 2012-06-04 11:30 -0400 |
| Message-ID | <jqikbn$v8l$1@dont-email.me> |
| In reply to | #15042 |
On 6/4/2012 10:57 AM, Roedy Green wrote: > On Sun, 03 Jun 2012 11:40:03 -0700, Roedy Green > <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted > someone who said : > >> has anyone benchmarked or decompiled to see how the new case string >> labels are compiled. > > I gather this was the confusing sentence. What should I have called > the feature? Using switch with Strings? -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-06-06 00:15 -0700 |
| Message-ID | <jqn02e$lk4$1@news.albasani.net> |
| In reply to | #15046 |
Joshua Cranmer wrote: > Roedy Green wrote: >>> has anyone benchmarked or decompiled to see how the new case string >>> labels are compiled. >> >> I gather this was the confusing sentence. What should I have called >> the feature? > > Using switch with Strings? Strings in switch is the Project Coin name. Also, string case labels. The JLS calls them "switch labels", "case constants" and "case labels".(§14.11) (Case constants actually are only the constants within the case label, but that still pertains to Roedy's question.) So do the programmers who implemented these constructs for Java. But I had no trouble understanding that Roedy meant "string case labels" for "case string labels". The mere word transposition seemed like a typo. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web