CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

switch case vs if else performance

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sudu



Joined: 23 May 2004
Posts: 7

View user's profile Send private message

switch case vs if else performance
PostPosted: Thu Oct 14, 2004 8:05 pm     Reply with quote

hi there,

just wanna to ask the different of switch and if-else operation performance. whitch one is faster.
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Thu Oct 14, 2004 8:08 pm     Reply with quote

Well it depends....

Kind of like asking which is faster a drag car or Nascar. It really depends on the application.

I would suggest you compile your code both ways if timing is critical and check.

Trampas
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 14, 2004 9:34 pm     Reply with quote

I recall I believe it was RJ doing some tests and found that coding switch statements a certain way caused the compiler to create a jump table which was faster. If's and switch's are pretty comparable but I think the switch is more readable.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Oct 15, 2004 8:19 am     Reply with quote

If your 'if()' sequence is very long then I would go with the switch() route. You could have, say, 10 if()'s that you're checking on and you have to compare each one to reach the last one. The switch() would do one comparison and then jump to the correct place without all of that wasted time. The switch() statement does create a fair amount of code though so, if size is not important then I would go with the switch().

Ronald
Ttelmah
Guest







PostPosted: Fri Oct 15, 2004 10:04 am     Reply with quote

Mark wrote:
I recall I believe it was RJ doing some tests and found that coding switch statements a certain way caused the compiler to create a jump table which was faster. If's and switch's are pretty comparable but I think the switch is more readable.

It toggles depending on the size of the table, and the presence of a 'default'. If you have _no_ 'default' statement, and code:
Code:

   tval=getc();
   switch (tval) {
   case 0:
      putc('0');
      break;
   case 1:
      putc('1');
      break;
   case 2:
      putc('2');
      break;
   case 3:
      putc('3');
      break;
   case 4:
      putc('4');
      break;
   case 5:
      putc('5');
      break;
   }

A jump table will be generated. However if you code only three cases, it uses normal tests instead.
If you add a 'default', then normal tests are used.
Speed wise, the 'test' version, is the same as a series of 'if' statements. It ges slower the further down the tree the final branch is placed. The jump table, has more initial overhead, but the time involved is constant, for any entry.
So if the number of choices is large, the jump table version will be significantly faster for all except the first few choices. This is why CCS keep 'with' the test version for very small numbers of options.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group