• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch case order affect the speed

 
Greenhorn
Posts: 8
Eclipse IDE Java ME Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering whether the order of switch case has something to do with speed of processing. Although i tried some but didn't see any difference, may be because of high speed processor or may be it has nothing to do with speed. Can anyone help.?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There may well be some small, and I mean really small, difference between matching the first case and the nth case in a switch statement. But it's going to be really difficult to predict what the differences are for a number of factors. Firstly each compiler may deal with the switch differently, and secondly each VM may optimise things differently. Different usage patterns for the exact same statement with the exact same compiler on the exact same VM may perform differently. You'll never be able to reliably predict the behaviour, nor should you need to.

The most important question to ask yourself here is Why should I care? Why do you need to know? If you're trying to optimise a particular piece of code because it's slow then I suspect you're looking at the wrong thing. If it's just through curiosity then forget about it. It's just not important. Move on.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+1 on not worrying about this.
 
Mangal Pandey
Greenhorn
Posts: 8
Eclipse IDE Java ME Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just asked out of curiosity nothing else. But thanks for your response.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mangal Pandey wrote:I just asked out of curiosity nothing else. But thanks for your response.




Out of curiosity, and if my memory serves correctly, a switch statement is supposed to use a jump table (when it was invented in C). This requires the switch variable to be of only certain types, and the case statements to be constants. Certain data types, such as a double can't be used, because the jump table would be too big (and difficult to optimize). And of course, without the case being constants, it was not possible for the compiler to build the table. Anyway, since it was a jump table, then in theory, it should be just as efficient regardless of which case statement it is switched to.

Java is modeled after C/C++ in regards to the switch statement. It has similar restrictions, in types of data that can switched, and the cases being constants.... then I guess we can speculate that it is also likely that the case order also does not affect the speed.

Henry
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you do is to compile a class with a switch statement in, then view the bytecode with javap -c Foo
That will confirm what Henry said about jump tables, and that there is no difference in speed.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that there are two different bytecode instructions that can be used when compiling a switch, depending on the distribution of case values you use. See JVMS7 3.10 Compiling Switches for more information. I'm pretty sure that for a lookupswitch, the order is completely irrelevant, since the compiler is required to reorder the values in sorted order anyway. For a tableswitch I suppose order could matter, but I doubt it. The obvious implementation would result in no difference at all in execution time, regardless of order.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mangal Pandey wrote:I just asked out of curiosity nothing else. But thanks for your response.


It might be worth mentioning that when writing if...else lists (which is what switch was created to avoid - in some cases), there can be a slight advantage to listing your if conditions in order of probability (ie, most likely first) if it's possible.

For example, when doing a binary chop, the "equal" condition is usually the least likely to occur, so it's generally left till last. That doesn't apply to a switch though, since the value is only evaluated once.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as the different conditions don't have side‑effects. Otherwise using condition 1 may modify the behaviour of condition 2.
You can get the same slight advantage if you put the condition more likely to be false left of the && operator or right of ||.
There are some instances where it is necessary to oder test, etc. If you use getClass() in an equals method, you must have the null test before it.If you have those terms in exactly that order, you can be sure that if it compiles it will run free from exceptions. The test for ob == this goes first, not because it is more likely to be true, but because it is very fast and can short‑circuit the method so the second half is never called.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic