Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if (with many conditions)Vs Switch

Status
Not open for further replies.

25884

Programmer
Mar 12, 2001
46
AP
Hi..
my problem is simple...i want to know which statement will result in more efficient and faster executing code..

if(char1.equals(':')||char1.equals(';')||char1.equals('.')||etc....)
{
//do something
}

OR

switch (char1)
case ':' : //dosomething
case ';' : //dosomething
case '.' : //dosomething
etc....

Please explain in terms of bytecode lines and execution speed and suggest me which one is better ..
Thanks
Sonali.
 
Sonali,

Had a thorough search and could not find a definitive answer. However I feel i have to point out some elimentary mistakes in your code

1)Primitives do not have methods, so you can't call equals() on a char. You have to use ==.

2)The if statement and the switch statement you have written would produce different results. You would EITHER have to change the if(stuff || stuff etc) to: if (stuff) {//do something} else if (stuff) {//do something} etc
OR change the switch to:
switch (char1)
case ':' :
case ';' :
case '.' :
//dosomething
etc....

Sorry to be pedantic, but the java part of me refused to let it go!!!

David

 
Well, I cant explain it in terms of byte code etc.. but here are my thoughts.

In the first instance (the big if statement) you need to go through each condition in turn until you get one that that equals true at which point the whole condition equals true and the appropiate branch is executed. if none ofthe conditions equals true then all of the conditions in the if statement are evaluted for nothing (waste of CPU time).

Using the switch statement means that the VM will go straight to the appropiate branch of execution, without having to evaluate all the other conditions first.

It strikes me (and please someone correct me if I'm wrong) that the switch statement is the neater of the two, plus it makes the code easier to read. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
If you intend to have the cases execute the same code, which would make the if (many ORs) equivalent to the switch statement, then my first guess would be that the if statement is faster but only with a ridiculous amount of tests and only noticeably so if you organize the tests most likely to be true to the far right.

Java does short-circuit evaluation, and in a compound OR statement, as soon as one test returns true all further evaluation is omitted. You could conceivably save time by putting more specific (or less likely to be true) tests to the left (as boolean evaluation is done right-to-left).

I think that at some level the switch statement is probably regarded as a compound OR statement anyway, and exists mainly as a convenience to programmers. I'm not sure how a compiler could translate a switch statement without ifs and ors anyway.

For a definitive answer, you'd obviously want a timer, nearly identitical stubs using both methods, and a ridiculously large input. I would expect the difference to be nominal, though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top