464,292 Members | 1,165 Online
Need help? Post your question and get tips & solutions from a community of 464,292 IT Pros & Developers. It's quick & easy.

# Which of switch statement and if-else statement takes less time to execute?

 P: n/a Which of switch statement and if-else statement takes less time to execute? Nov 15 '05 #1
18 Replies

 P: n/a sw*******@gmail.com wrote on 29/07/05 : Which of switch statement and if-else statement takes less time to execute? There is no definitve answer to this question. Dd some tests and measurements in known conditions. -- Emmanuel The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html The C-library: http://www.dinkumware.com/refxc.html "C is a sharp tool" Nov 15 '05 #2

 P: n/a (supersedes ) sw*******@gmail.com wrote on 29/07/05 : Which of switch statement and if-else statement takes less time to execute? There is no definitve answer to this question. Do some tests and measurements in known conditions. -- Emmanuel The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html The C-library: http://www.dinkumware.com/refxc.html ..sig under repair Nov 15 '05 #3

 P: n/a In article <11*********************@g44g2000cwa.googlegroups. com>, wrote: Which of switch statement and if-else statement takes less time to execute? The difference between them pales in comparison to other issues such as algorithm choice. That being said, switch and if-else do different things. If you have a two-way condition, use if-else. If you have three or more cases, use switch. Trying to express a switch as a chain of if-else's is unlikely to be better than a switch statement. The only time this should be considered is if you know that one case out of many is statistically far more likely to occur. Then you might consider testing for that one case and using switch on the rest. -- 7842++ Nov 15 '05 #5

 P: n/a "Keith Thompson" wrote in message news:ln************@nuthaus.mib.org... "Bhan" writes: I am trying the same.Please help me out Thanks for your reply Bhanu You are trying the same as what? Provide some context. Don't assume that we can see the article to which you're replying. He didn't have enough time to provide context... There was only 5 minutes left for him to complete his online quiz and he needed the answers quick! Besides, the question was asked in the message subject... can't you read? :-) Nov 15 '05 #7

 P: n/a In article <11*********************@g44g2000cwa.googlegroups. com>, sw*******@gmail.com wrote: Which of switch statement and if-else statement takes less time to execute? Use a profiler. Try both ways and measure the difference in speed. If you don't have a profiler, call the function you try to improve hundred times instead of once only and use a stopwatch. Or call it thousand times. Then measure. Nov 15 '05 #8

 P: n/a wrote Which of switch statement and if-else statement takes less time to execute? Just depends. A very short switch will probaly be compiled to exactly the same code as the corresponding if ... else. A longer switch may use a jump table, if the constants in the case are small enough. Usually this will be a little bit faster than if ... else. Nov 15 '05 #9

 P: n/a Anonymous 7843 wrote: Trying to express a switch as a chain of if-else's is unlikely to be better than a switch statement. The only time this should be considered is if you know that one case out of many is statistically far more likely to occur. ....or if the condition may change between tests and you want to take it into account. I would not recommend it, though. It is hard to debug. Peter Nov 15 '05 #10

 P: n/a swaroo...@gmail.com wrote: Which of switch statement and if-else statement takes less time to execute? This is the wrong question to ask. You don't decide to use a switch over an if-else chain (or vice-versa) based on performance; for one thing, the performance difference, if any, will not be consistent from case to case. Secondly, the intent of the code may be obscured by using an inappropriate construct, making it harder to read and understand. Use a switch when you need a multiway branch based on a single integral value; use an if-else chain for everything else. For example, writing switch(a < b) { case 1: /* true */ /* do something */ break; case 0: /* false */ /* do something else */ break; } is silly and a little obfuscatory even if it buys you an extra cycle; writing if (a < b) /* do something */ else /* do something else */ is clearer and more straightforward. By contrast, even though there's nothing wrong with writing if (a == 1) /* do something */ else if (a == 2) /* do something else */ else if (a == 3) /* do yet another thing */ .... this is the kind of situation for which the switch statement was invented in the first place: switch(a) { case 1: /* do something */ break; case 2: /* do something else */ break; case 3: /* do yet another thing */ break; ... } Nov 15 '05 #11

 P: n/a If you know the answer tell me. Else keep quite. Mark wrote: "Keith Thompson" wrote in message news:ln************@nuthaus.mib.org... "Bhan" writes: I am trying the same.Please help me out Thanks for your reply Bhanu You are trying the same as what? Provide some context. Don't assume that we can see the article to which you're replying. He didn't have enough time to provide context... There was only 5 minutes left for him to complete his online quiz and he needed the answers quick! Besides, the question was asked in the message subject... can't you read? :-) Nov 15 '05 #12

 P: n/a In article <11**********************@g47g2000cwa.googlegroups .com>, Bhan wrote:If you know the answer tell me. The answer is 42. Else keep quite. Quite what? Nov 15 '05 #14