Thread: skipping "else" statement is faster?

  1. #1
    coder
    Join Date
    Feb 2008
    Posts
    127

    skipping "else" statement is faster?

    hello
    take these two function examples which should do the same thing:
    Code:
    // standard way
    bool function () {
    	if (a == 0) {
    		return false;
    	}
    	else if (a > 0){
    		return max (a);
    	}
    	else {
    		return min (a);
    	}
    }
    
    // non-standard way
    bool function () {
    	if (a == 0) {
    		return false;
    	}
    	if (a > 0) {
    		return max (a);
    	}
    	return min (a);
    }
    It's obvious that the first way is easier to read and way better to do things
    I am wondering if the second way would be faster once compiled, or not.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That would depend on your compiler, and what options you have set for it.

    I can't agree that the first way is "way better", at all. The second way seems very "clean" and perfectly straightforward.

    Unless you're compiler is smart, I'd bet on the second way being faster - but what are you really talking about for a difference here? I LIKE nit-picking, but here we're approaching something probably in the thousands of a second, or less?

    My enthusiasm wanes at that point.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    A smart compiler should be able to optimize either function to the same assembly code I believe.

  4. #4
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by adak
    but what are you really talking about for a difference here?
    I LIKE nit-picking, but here we're approaching something probably in the thousands of a second, or less?
    It's probably much less than a thousand of second, but if I'd had to call that function 324,153,423,484 times, that wuold make some difference.
    By the way, it's more funny if I always search for the better way to do something

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Aye, on gcc-4.1.2, they both create the same assembly code, I'd assume most other modern compilers would do the same.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by MacGyver
    A smart compiler should be able to optimize either function to the same assembly code I believe.
    Is the gnu compiler (gcc - g++) supposed to be "smart"?

    edit: oops I posted this before reading the post above ^^
    Thanks all, my question is answered

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    On a side note, some coding standards, such as that of Mozilla, require you to omit redundant elses, i.e. to go for option #2 in this scenario. I'm not sure about the exact reasoning behind that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    As others have said, most modern quality compilers will do the same thing, regardless of how the function is coded. The choices are stylistic, not functional.

    Those who dislike multiple returns from functions would probably do this;
    Code:
    bool function ()
    {
    	bool retval = false;
            if (a > 0)
                retval = max(a);
            else if (a < 0)
                retval = min(a);
            return retval;
    }
    Again, any reasonably modern/quality compiler will probably do the same with this as with other examples above.

    The reason for omitting redundant elses in code is related to proveability and elimination of dead code: it is more difficult to formally prove if a redundant else clause is or is not entered.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by carlorfeo View Post
    hello
    take these two function examples which should do the same thing:
    Code:
    // standard way
    bool function () {
    	if (a == 0) {
    		return false;
    	}
    	else if (a > 0){
    		return max (a);
    	}
    	else {
    		return min (a);
    	}
    }
    
    // non-standard way
    bool function () {
    	if (a == 0) {
    		return false;
    	}
    	if (a > 0) {
    		return max (a);
    	}
    	return min (a);
    }
    It's obvious that the first way is easier to read and way better to do things
    I am wondering if the second way would be faster once compiled, or not.
    I'd say the first way is better.
    In this particular case you have return statements in the if statements, so it wouldn't make much difference, but if you didn't return, the first method would skip all other else statements as soon as it finds one that evaluates to true, while the 2nd example would evaluate every if statement.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    A smart compiler should be able to optimize either function to the same assembly code I believe.
    Even a dumb compiler should. On the spectrum of the complexity of things which can be optimized, this sort of thing ranks about "0".

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by brewbuck View Post
    Even a dumb compiler should. On the spectrum of the complexity of things which can be optimized, this sort of thing ranks about "0".
    Dumb compilers are usually termed such because they fail to achieve even standard compliance.... any optimizations by a dumb compiler would be shocking and somewhat above what we expect of them.

    But on a serious note, I do get your point.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by brewbuck View Post
    Even a dumb compiler should. On the spectrum of the complexity of things which can be optimized, this sort of thing ranks about "0".
    I fully agree.
    This is one of the easiest things a compiler can optimise.

    Use whatever you prefer. It is not really a question of speed here, but a question of style.
    The second listing can perhaps be more obvious that all control paths return values.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    Even a dumb compiler should. On the spectrum of the complexity of things which can be optimized, this sort of thing ranks about "0".
    As long as the compiler has a "optimize" option (that actually does ANYTHING), it should cope with that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This is one of the easiest things a compiler can optimise.
    Not quite true. The easiest things to optimize are constant folding, instruction order optimization, instruction choice optimization, construct replacement (e.g. replace &#37;2 with &1 where allowed), and similar optimizations. These have the property that they don't need instruction flow analysis. Instruction reordering needs a very simple form of data flow analysis.

    This optimization needs control flow analysis. Granted, once you have that, this form of dead code removal is trivial, but first you need the analysis.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  2. Can repeated characters be printed with a C statement?
    By Pete_2000 in forum C Programming
    Replies: 4
    Last Post: 05-05-2006, 07:47 PM
  3. does const make functions faster?
    By MathFan in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 09:03 AM
  4. if is faster than switch?
    By skorman00 in forum C++ Programming
    Replies: 32
    Last Post: 03-06-2004, 01:15 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM