I'd say let the compiler and runtime worry about this. Even if there is a difference -and if there is one, the answer could differ between different versions of the JDK- this is not something you should base decisions on. What prompted this question?
Ping & DNS - updated with new look and Ping home screen widget
Joined: Dec 26, 2008
just a random question...
i did search google and they say switch is faster... but i also think that there is little difference....
so i really don need to bother about the efficiency here? is the efficiency difference really negligible?
It's simple Performance depends on the breaking condition. I think that if else and switch have the same performance if the breaking condition is in the same level. In order to test that, let's have a 1000 times loop and an if condition. The same test using switch. But don't forget to output the entering and exiting method timestamp to get the timing. You'll see that performance is the same.
The most important thing to remember is to code your code for clarity. Minor differences in performance are moot. It's only when gross inefficiencies are detected that you should consider optimizing your code with the aid of profiler to exactly pinpoint where bottlenecks may lie.