tags:

views:

186

answers:

7

In Java: Which is the most performance code and why?

if (x==1) {
    ....
} else if (x==2) {
    ....
} else if (x==3) {
    ....
} else if (x==4) {
    ....
}
... rest code here...

or 

try {
    if (x==1) {
        ... 
        throw MyException(1);
    }
    if (x==2) {
        ... 
        throw MyException(2);
    }
    if (x==3) {
        ... 
        throw MyException(3);
    }
    if (x==4) {
        ... 
        throw MyException(4);
    }
} catch(MyException MEx) {
    ... rest code here ...
}

Thanks a lot!

A: 

At the moment, the two are not comparable. Why put the "rest of code" in the second example in an exception handler, when that exception is not mentioned in the first example? If that exception is certain to be thrown, then "rest of code" in the first will never be executed.

Adam Wright
+13  A: 

In your example, you are using exceptions for flow control. Exceptions are expensive operations (or, at least, significantly more expensive than if and else-if blocks). Exceptions should only be used for exceptional situations.

Also, using else if will ensure that the checking stops as soon as a matching case is found. Using just if will check all of them all the time, and any matching cases will be executing. If you have many cases and they are ordered by the most common cases toward the top, using if-else-if will perform better.

Thomas Owens
+1 for commenting that the two blocks actually do different things.
Adriano Varoli Piazza
Usign if with throw i have no to execute other cases, any way... now it's clear for me that exceptions are more expensive than else if. Thanks a lot Thomas!
Mblua
A: 

You get more performance when not using exceptions. But exceptions are special kind of structure and not to be confused with control flow structures such as if-else.

zzandy
+2  A: 

Not only will the second version have worse performance, it's also quite confusing to the reader and not how exceptions are meant to be used.

sepp2k
+4  A: 

Others have answered your question, but I thought I'd point out...

Just because Java doesn't have the "goto" command (implemented), doesn't mean you should use Exceptions to simulate them.

RHSeeger
A: 

Not that I'm a big fan of switch statements but isn't this like a textbook situation to use them?

 switch (x) {
 case 1:

  break;
 case 2:

  break;
 default:
  break;
 }
...
NickDK
It depends. In this particular example, since everything is compared using ==, a switch would work. However, if you wanted to potentially execute multiple blocks, using ==, <, >, <=, and >=, then the if/else-if would be better.
Thomas Owens
Nick, you messed up your switch statement, you only need one switch(x) and add a default:
DanM
Thanks Dan, trying to be fast isn't always a good idea ;-)
NickDK
A: 

...Which is the most performance code

The if/elseif construct.

and why?

Because the later involves the creation of an object of type MyException ( which internally may fill the stacktrace, allocate memory for the object, assign variable with the exception code )

Although the difference may not perform that worst at first sight, it is definitely a bad use of exception.

I could give you ( or try to ) an explanation about it, but better is for you to read it from an authority on the matter:

Effective Java item: Use exception for exceptional conditions.

By Joshua Bloch.

OscarRyz