Help with casts

Garry Garrett garry at ceco.ceco.com
Sun Feb 24 04:26:53 AEST 1991


In article <339 at smds.UUCP>, rh at smds.UUCP (Richard Harter) writes:
> In article <409 at ceco.ceco.com>, garry at ceco.ceco.com (Garry Garrett) writes:
> > In article <1991Feb21.040145.8678 at cec1.wustl.edu>, abed at saturn.wustl.edu (Abed M. Hammoud) writes:
> 
> > > 		for (i=0; i < 100; i++)
> > > 		  x += i; 
> 
> > Consider:
> > 
> > 		for (i=99; i; i--)
> > 			x += i;
> 
> This is better done as 
> 
> 		for (i=100;--i>=0;)
> 			x += i;
> 

	I must disagree with you.  For starters, consider the situation where
i == 0.  Your loop adds 0 to x.  Secondly, as my discussion that you deleted
pointed out, performing "--i>=0" would take more time than "i"  The reason is
that both i and 0 must be loaded into registers, compared, and a branch 
instruction executed for your version.  My version loads i into a register
and performs a branch.  2 steps vs. your 4 steps.  Is I pointed out making i
a register variable would also speed things up.  You did have a good point
though about moving the deciment inside of the condition:

		for(i=100; --i; )
			x += i;

	This would be even faster than my proposed solution.
	Bear in mind that C is one of the hardest languages in the world to
optimize.  For instance, in pascal the index of a for loop cannot be referenced
outside of that loop, thus the code can be optimized by making that index a
register variable.  In C, you can reference i later in the code, so the compiler
cannot make that assumption for you.  You must explicitly ask the compiler
to make a varible into a register variable by declairing it as such.  There
are many reasons  why C is harder to optimize, so when you are programming in
C, you must not count on an optimizer to clean things up for you.
	If your computer's CPU's ALU has an option to compare a register to 0,
and you assembler has a command to use that option, and your C compiler knows
that that option exists, and your C compiler is smart enough to optimize --i>=0
to make use of that assemby instruction, then your method would compile to:
load i into a register, compare it to 0, conditional branch.  3 instructions to
my 2.  Sorry, but my code is still faster.

Garry Garrett



More information about the Comp.lang.c mailing list