• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

"if" vs. "switch", age old question-which is faster?

 
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O.K. - I'll bet this has started many a flame war in the past, but I just want to know everyones opinion.
Here is the situation. I am checking a value that I get back from a server. There are about 20 different values that might be returned and that I need to check for.
Question is, which is faster for this situation, "if" or "switch" or none of the above? I know we're talking nano-seconds but I really need the performance for this app.
Thx in advance,
Matt
 
Trailboss
Posts: 22972
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually try to find a way to avoid either. Can this problem be better solved with an array or polymorphism?
To answer the question directly: I dunno. If I had to pick between the two, I would go with a switch statement for reasons other than performance. But if performance is really the key, you might want to run JProbe. From an assembly language perspective it is possible that an optimization could be done on the switch statement.
 
Matt Midcap
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is JProbe?
 
paul wheaton
Trailboss
Posts: 22972
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JProbe is a profiler. You run your program under JProbe and it will tell you what the biggest time pigs in your system are.
So you could make two little programs, one with switch and one with if/else. Run both progams a million times under JProbe and see which one is faster.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple programs can give false results because of java optimises byte code.
For example, excuting 5000 sequences of
<pre>
if (a>b) {
i=$i;
}
</pre>
resulted to 0 ms, but executing 5000 of
<pre>
if (a+$i>b) {
i=$i;
}
</pre>
gave 4 ms, $i was substitued with iteration number. Of course this can be because of additional (+) operation, but 'add' is really faster than 'jmp'...
And I believe, that test programs can't reflect the real life if there is an optimizer exists.
By the way, inserting 10000 of the following blocks:
<pre>
if (a>b) {
i++;
i--;
} else {
i++;
i--;
}
</pre>
gave javac optimizer to throw StackOverflow exception >:O

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )

 
paul wheaton
Trailboss
Posts: 22972
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would that overflow the stack?
 
Pawel Veselov
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, seems it optimize the code recursivly. When it meets some instructions it does optimize on the next instruction. This is the trace:
at sun.tools.asm.Assembler.optimize(Assembler.java)
at sun.tools.javac.SourceClass.compileClass(SourceClass.java)
at sun.tools.javac.SourceClass.compile(SourceClass.java)
at sun.tools.javac.Main.compile(Main.java)
at sun.tools.javac.Main.main(Main.java)
Alas, I don't have this classes compiled with debug info, so I don't know exact cause for that exception...
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!