#### Archived

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

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

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

## 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 on other sites
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 on other sites
Using else-ifs are certainly faster since you then avoiding doing more condition checks.

##### 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 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.

 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 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 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 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 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 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

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633685
• Total Posts
3013323
×