Path: utzoo!utgpu!attcan!uunet!steinmetz!davidsen From: davidsen@steinmetz.ge.com (William E. Davidsen Jr) Newsgroups: comp.lang.c Subject: Re: C IF statement efficiency... Message-ID: <11897@steinmetz.ge.com> Date: 18 Aug 88 16:31:02 GMT References: <8808171400.AA05122@ucbvax.Berkeley.EDU> Reply-To: davidsen@crdos1.UUCP (bill davidsen) Organization: General Electric CRD, Schenectady, NY Lines: 25 In article <8808171400.AA05122@ucbvax.Berkeley.EDU> U23405@UICVM writes: | I was wondering if, of the two following program fragments, which one would | be compiled more efficiently by most C compilers: | | if (big > small) if (big > small) | return big; return big; | else return small; | return small; | | In other words, does a compiler handle IF statements with RETURNs more or less | efficiently than IF..ELSE statements with RETURNs (or with other statements | besides RETURN, for that matter) ? The ability to handle return at the end of an if (and not generate transfers) is one of quality of implementation. How about return (big > small ? big : small); I would expect this to completely avoid the problem. Even if a compiler does generate bad code for your 'else' example above, the extra transfer around the else clause is not executed, so it doesn't effect the performance. No matter how you write the source code, some compiler will generate inefficient or even non-functional object. -- bill davidsen (wedu@ge-crd.arpa) {uunet | philabs | seismo}!steinmetz!crdos1!davidsen "Stupidity, like virtue, is its own reward" -me