Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jitspoe

if's vs else-if's -- efficiency

This topic is 5956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just curious if there was much of a difference in instruction count using ifs vs else-ifs. Obviously you stop all the proceeding comparisons if a condition is met, but if you have conditions that are rarely met, like:
if(!strcmp(text,"this string rarely occurs"))
   doSomething();
else if(!strcmp(text,"this rarely occurs either"))
   doSomethingElse();
else if(!strcmp(text,"this may never happen"))
   andSoOn();
 
Would it be better to use "if"s there, since none of them can happen at the same time? Just a random thing I wonder. [edited by - jitspoe on May 30, 2002 10:55:32 PM]

Share this post


Link to post
Share on other sites
Advertisement
I would think it would be better to use if-else clauses if none of the conditions can happen at the same time.

Because if you use an if clause followed by ''n'' if-else clauses (where n is any real number); then you will only do 1<=x<=n+1 comparisons as most (not including if one of the if clauses has multiple comparisons).

Whereas if you have ''n'' single if clauses (without the else, and not nested) then there will be ''n'' comparisons regardless of whether any evaluate to true or not.



------

Shop for the Lowest Price!
Then, Check a Reseller''s Rating Before You Purchase!

Share this post


Link to post
Share on other sites
DUH!!!!


  
if () {} - (if condition met) -----\
else if () {} - (if condition met) ---\ |
... | | (goto)
else if () {} - (if condition met) -\ | |
. . .


Look at the assembly being generated. Always fun and educational

------------------------
CRAZY_DUSIK* pCrazyDuSiK = new CRAZY_DUSIK;
pCrazyDuSiK->EatMicroshaft(MS_MUNCH_BILL_GATES | MS_CHEW_BILL_GATES);

Share this post


Link to post
Share on other sites
Actually, it would be better if you checked for the most often occuring condition first. Say if this code snippet was entered 500,000 times and the other conditions only happened say 100 times, you would have to go thru those first 2 ifs 499,900 (a total of 999800 ifs) to get to that last if.

As far as # of actual instructions executed for ifs vs else ifs, I don't really know. But it would stand to reason that it would be less using if else IF the most often occuring conditions were checked for first.

[edit] Man, I can't spell tonight.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on May 30, 2002 11:22:02 PM]

Share this post


Link to post
Share on other sites
Yay Chem0sh is like the only *smart* person here

------------------------
CRAZY_DUSIK* pCrazyDuSiK = new CRAZY_DUSIK;
pCrazyDuSiK->EatMicroshaft(MS_MUNCH_BILL_GATES | MS_CHEW_BILL_GATES);

Share this post


Link to post
Share on other sites
Hm and as to why I even bother to reply to this thread is cause I''m bored out of my mind right now... too drunk to code anyway

------------------------
CRAZY_DUSIK* pCrazyDuSiK = new CRAZY_DUSIK;
pCrazyDuSiK->EatMicroshaft(MS_MUNCH_BILL_GATES | MS_CHEW_BILL_GATES);

Share this post


Link to post
Share on other sites
Thanks, dusik! Nice sig, BTW.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on May 30, 2002 12:08:46 AM]

Share this post


Link to post
Share on other sites
yeah, you want to order them according to which is the most common, but I was saying if NONE of them occured very often (maybe needed for error checking or something that will happen once every 100 times you run the program -- hypothetical)...

I guess the only difference the "else" makes is a branch at the end -- a tiny bit of mem taken up -- but doesn''t affect the number of instructions if all of the conditions are false. It was just on my mind, heh -- thought I''d throw it out there. I don''t even remember what I was doing.

Share this post


Link to post
Share on other sites
O.K. and what about switch? How it is working, like if-else or like if, if, ... construction?
Cheers to Dusik

Lekha

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!