C IF statement efficiency...

William E. Davidsen Jr davidsen at steinmetz.ge.com
Fri Aug 19 02:31:02 AEST 1988


In article <8808171400.AA05122 at ucbvax.Berkeley.EDU> U23405 at 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 at ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.lang.c mailing list