Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Both nested if else and switch case generate the same output.How internally both work?The search mechanism is it different or same and which one of these is efficient?
Posted
Comments
enhzflep 9-Nov-13 1:13am    
Do you want the short answer, or the long answer? :grin:

Short: It depends. It depends, it depends.

Long: However the producer of the particular compiler used decides to implement it. Depending on the ability of the compiler to optimize the code, they may be identical or very different. Efficiency depends both on the method of implementation and the number of conditions being checked.

This is a theoretical concern only, for the most part. The expense of comparisons is minuscule when compared to the power available to modern CPUs and to the total computational power need by the stunning majority of programs. This holds true for all chips I've used, whether they be a $2 Atmel ATMega or a several $100s Intel whatever, or a MIPS R300.

I forget the switch, but you can tell the compiler to leave you a copy of the generated assembly code when you compile your C code. You can then directly compare the two listings.
SubhamSoni123 9-Nov-13 11:32am    
The reason I posted this question is that my professor told handling exceptions are better than handling if else..if constructs(they take more time)... and the way exceptions should be handled is similar to the switch case(searching the exception class == searching the case under which the condition is true) so if my case ranges are compact then I would rather go for if else if. giving the range there.. why in switch?Can you please explain in detail?
enhzflep 9-Nov-13 20:37pm    
If you wish to reply to a comment, hover over the comment and hit the silver [Reply] button. If you dont, the person to whom the comment is addressed will not get a notification.

As for your comment, using exceptions can make the code considerably clearer and easier to read. It can be much easier to break out from a block of code with an exception, than it is to pass an error-value back-up the call stack.
E.g consider a function that calls another that calls another - do this until you're (say, for example) 10 layers deep. A problem there can throw an exception which will be dealt with immediately. Not using exceptions means that you'll have to pass the error ccode back through the 10 calling functions.
SubhamSoni123 9-Nov-13 23:03pm    
I understand this.. but now the confusion I have is the search mechanism that is used.It is the same old binary search or some kind of indexing or hashing?How do I find it out?
enhzflep 9-Nov-13 23:33pm    
"Is it the same old binary search or some kind of indexing or hashing?"
I don't know.

"How do I find out?"
I'd suggest asking the question on a site that was less Microsoft-centric. Stackoverflow.com users can typically provide good answers - one must be careful when asking questions there, users are typically much less tolerant of poorly-phrased questions than are CP users (I'm not implying anything, just stating an observation)

Failing that (the approach of asking at a site whose users are better placed to answer such technical questions), I can see a number of options.

1) Ask the question again here, being specific about the fact that you're interested in the mechanism by which the appropriate exception is selected.

2) Download the source-code for gcc and have a look yourself, the answer (for how gcc does it - other vendors may vary in their approach. I doubt it's mandated by the c++ specification) is certainly there. It may be like searching for a needle in 10 haystacks though.

3) Code a minimal program that uses exceptions, build it with the flag that tells the compiler to retain the intermediate ASM code, then look through that code for your answer.
This may be like searching for a needle in 3 or 4 haystacks, I don't know.

That would be how I'd approach such a search for information on the topic. Whether or not it would be successful or quick is another matter - I've no idea.

1 solution

The idea of the switch statement is to do a computed branch. In the ideal case a switch statement can be compiled into just a few instructions and a branch table; it takes constant time for every of the cases instead of linear time for a lengthy if elseif elseif ... construct.

Unfortunately, the case values are sometimes spread over a range of values that would not allow the creation of a branch table or would at least be space prohibitive. In such cases a good compiler generates code that is a combination of several distinct branch tables and some conventional "if elseif" code. So in the worst case, a switch statement could even be slower than the corresponding if elseif.

Compiler developers have invented quite a few more tricks to map even widely distributed branch values into effivient code.

So the idea is, if you have many branch targets then use a switch statement and it will be much more efficient than the corresponding if elseif construct, given that the case values fall into a compact interval.
 
Share this answer
 
Comments
SubhamSoni123 9-Nov-13 11:32am    
The reason I posted this question is that my professor told handling exceptions are better than handling if else..if constructs(they take more time)... and the way exceptions should be handled is similar to the switch case(searching the exception class == searching the case under which the condition is true) so if my case ranges are compact then I would rather go for if else if. giving the range there.. why in switch?Can you please explain in detail?
nv3 10-Nov-13 7:31am    
Your are talking about a "search mechanism" to select the proper catch clause of an exception. As far as I know, there is no such thing as a search mechansim. Catch clauses must be tested in the sequence they are specified in the source code.

About the "handling exceptions are better than handling if-else constructs (they take more time)": That must be a big misunderstanding. I don't know what your professor is referring to as "better". Sometimes, using exceptions is a better method than passing up return codes over multiple layers of nested functions (as enhzfle explained in his comments above). If "better" is meant as "faster", then I would say: Definitely not! Throwing an exception is a relatively time-consuming operation. It is meant to be used in those rare cases that something out of the ordinary happens in your program. Exceptions should not be used for things that happen regularly in your program, for example the end of an iteration.

As for the speed comparison between if-else and switch statements: A switch statement with a compact range of cases (say 1000 cases numbered 1 ... 1000) is magnitudes faster than the corresponding if-elsif-elseif construct -- except you have a very smart compiler that recognizes the regular structure of such an if-elseif chain and converts it into the same machine code as used of the switch statement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900