views:

971

answers:

6

Worrying about my web application's performances, I am wondering which of "if/else" or switch statement is better regarding performance?

+21  A: 

That's micro optimization and premature optimization, which are evil. Rather worry about readabililty and maintainability of the code in question. If there are more than two if/else blocks glued together or its size is unpredictable, then you may highly consider a switch statement.

Alternatively, you can also grab Polymorphism. First create some interface:

public interface Action { 
    void execute(String input);
}

And get hold of all implementations in some Map. You can do this either statically or dynamically:

Map<String, Action> actions = new HashMap<String, Action>();

Finally replace the if/else or switch by something like this (leaving trivial checks like nullpointers aside):

actions.get(name).execute(input);

It might be microslower than if/else or switch, but the code is at least far better maintainable.

As you're talking about webapplications, you can make use of HttpServletRequest#getPathInfo() as action key (eventually write some more code to split the last part of pathinfo away in a loop until an action is found). You can find here similar answers:

If you're worrying about Java EE webapplication performance in general, then you may find this article useful as well. There are other areas which gives a much more performance gain than only (micro)optimizing the raw Java code.

BalusC
Thanks for your input ;)
Anth0
or consider polymorphism instead
jk
That's indeed more recommended in case of "unpredictable" amount of if/else blocks.
BalusC
I'm not so quick to dismiss all early optimization as "evil". Being too aggressive is foolish, but when faced with constructs of comparable readability choosing one known to perform better is an appropriate decision.
Brian Knoblauch
The HashMap lookup version can easily be 10 times slower compared to a tableswitsch instruction. I wouldn't call this "microslower"!
x4u
I mean, slower in terms of "microseconds".
BalusC
@Brian: **Thank You!!**
Software Monkey
+1 for "micro optimization".
fastcodejava
+13  A: 
John Feminella
+1. There is a good chance that time spent on network I/O is easily eclipsing this particular issue.
Adam Paynter
A: 

I totally agree with the opinion that premature optimization is something to avoid.

But it's true that the Java VM has special bytecodes which could be used for switch()'s.

See WM Spec (lookupswitch and tableswitch)

So there could be some performance gains, if the code is part of the performance CPU graph.

Waverick
+3  A: 

According to Cliff Click in his 2009 Java One talks A Crash Course in Modern Hardware:

Today, performance is dominated by patterns of memory access. Cache misses dominate – memory is the new disk. [Slide 65]

You can get his full slides here.

Cliff gives an example (finishing on Slide 30) showing that even with the CPU doing register-renaming, branch prediction, and speculative execution, it's only able to start 7 operations in 4 clock cycles before having to block due to two cache misses which take 300 clock cycles to return.

So he says to speed up your program you shouldn't be looking at this sort of minor issue, but on larger ones such as whether you're making unnecessary data format conversions, such as converting "SOAP → XML → DOM → SQL → …" which "passes all the data through the cache".

Jim Ferrans
A: 

You should give this a read.

Leo Jweda
+1  A: 

I remember reading that there are 2 kinds of Switch statements in Java bytecode. (I think it was in 'Java Performance Tuning' One is a very fast implementation which uses the switch statement's integer values to know the offset of the code to be executed. This would require all integers to be consecutive and in a well-defined range. I'm guessing that using all the values of an Enum would fall in that category too.

I agree with many other posters though... it may be premature to worry about this, unless this is very very hot code.

mlaverd