2

I was wondering, In Java, in a switch statement what is the access time needed to reach the block of code identified by the input switch value?

  • It is a complete sequential search?

  • it is a direct access? And if so, how it's implemented?

Luca Vitucci
  • 3,674
  • 4
  • 36
  • 60
  • 4
    This is up to the compiler + JVM. – Oliver Charlesworth Dec 29 '13 at 19:01
  • why does it matter? even if switch-case does not use a lookup table and runs sequential search most of the time you use fairly small set of cases. BTW it is really easy to test, did you test it? – aviad Dec 29 '13 at 19:07
  • 1
    A lot of material and answers can also be found on [SO](http://stackoverflow.com/questions/15621083/why-does-java-switch-on-ordinal-ints-appear-to-run-faster-with-added-cases/15621602#15621602) – Meno Hochschild Dec 29 '13 at 19:08
  • @MenoHochschild Your find goes for a duplicate for this question. It explains everything---and then some---that OP has asked here. – Marko Topolnik Dec 29 '13 at 19:10
  • Someone told me that if you have keys like 1, 2, 3, 500000000 (4 keys) you would have performance problems because some compiler will create a huge direct access table. It sounds strange to me. – Luca Vitucci Dec 29 '13 at 19:10
  • Somebody severely underestimates the intelligence and skill of compiler writers. It ain't necessarily so. The same somebody should *try* it sometime instead of just guessing, and certainly instead of passing off his guesswork on you as fact. – user207421 Dec 29 '13 at 23:07
  • HotSpot Server VM jumps there directly if it's reasonable to do so. HotSpot Client VM doesn't, because it's too stupid to do anything right. – Boann Dec 29 '13 at 23:13
  • fun part, this somebody is a master degree's professor in computer sciences -.-' – Luca Vitucci Dec 30 '13 at 03:08
  • So? On this evidence, he knows diddly-squat about compiler construction. I was using compilers over 20 years ago that had more sophisticated strategies than he appears to be aware of, and I was writing them before that. – user207421 Dec 30 '13 at 08:36
  • I'm just saying that's outrageous that someone like him is able to teach at graduate level :) – Luca Vitucci Dec 30 '13 at 11:01
  • I agree. Don't be intimidated by academic degrees. Ask yourself why is he teaching instead of doing? and when did he last actually do it? If ever? and if he did, how out of date is his experience? – user207421 Dec 31 '13 at 00:12

1 Answers1

0

"Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing. An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object." - JavaDocs http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

"It uses the lookupswitch JVM instruction, which is essentially a table lookup" - How does Java's switch work under the hood?

Worrying about 'access time is micro optimization and premature optimization, which are evil. Rather worry about readabililty and maintainability of the code in question.' - What is the relative performance difference of if/else versus switch statement in Java?

Community
  • 1
  • 1
TheRed__
  • 164
  • 11