[comp.lang.c] C IF statement efficiency...

U23405@UICVM (08/13/88)

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

                                                 Michael Steiner
                                                 Email: U23405@UICVM
 

sullivan@vsi.UUCP (Michael T Sullivan) (08/18/88)

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;

Let's throw

	return (big > small ? big : small);

into the consideration pile as well.

-- 
Michael Sullivan				{uunet|attmail}!vsi!sullivan
V-Systems, Inc. Santa Ana, CA			sullivan@vsi.com
"Your mother was a hamster and your father smelled of eldeberries!  Pbbbt!"

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (08/18/88)

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

lee@ssc-vax.UUCP (Lee Carver) (08/19/88)

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

Just to muddy the waters, for this application you can use

	return ( big > small ? big : small )

But, maybe you have a real body of code to execute?

henry@utzoo.uucp (Henry Spencer) (08/19/88)

In article <8808171400.AA05122@ucbvax.Berkeley.EDU> U23405@UICVM writes:
>          if (big > small)             if (big > small)
>               return big;                  return big;
>          else                         return small;
>               return small;

Most C compilers should generate precisely the same code for these.
Really dumb ones might generate an extra "return;" after the lefthand
code, not realizing that it was unreachable.
-- 
Intel CPUs are not defective,  |     Henry Spencer at U of Toronto Zoology
they just act that way.        | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

ben@bosco.UUCP (ben ullrich) (08/20/88)

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

how about
	return ((big > small) ? big : small)

i don't know how efficient this is, but it is certainly more succinct than
those suggested in the quoted text above.


...ben
--
ben ullrich						sybase, inc.
database administrator					6475 christie avenue
mis department						emeryville, ca  94608
{pyramid,pacbell,sun,mtxinu,capmkt}!sybase!ben		415 - 596 - 3654

limes@ouroborous (Greg Limes) (08/24/88)

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 article <566@sybase.sybase.com>, ben@bosco (ben ullrich) writes:
>how about
>	return ((big > small) ? big : small)

>i don't know how efficient this is, but it is certainly more succinct than
>those suggested in the quoted text above.

All three of these examples reduce to the same thing quite early on in the
compilation; in fact, the compiler on my workstation (SunOS 4.0) produces code
for them that differs only in the names of some of the temporary labels used.
--