Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!ceco!garry From: garry@ceco.ceco.com (Garry Garrett) Newsgroups: comp.lang.c Subject: Re: Help with casts Message-ID: <414@ceco.ceco.com> Date: 23 Feb 91 17:26:53 GMT References: <1991Feb21.040145.8678@cec1.wustl.edu> <409@ceco.ceco.com> <339@smds.UUCP> Organization: Commonwealth Edison Co., Chicago, IL Lines: 47 In article <339@smds.UUCP>, rh@smds.UUCP (Richard Harter) writes: > In article <409@ceco.ceco.com>, garry@ceco.ceco.com (Garry Garrett) writes: > > In article <1991Feb21.040145.8678@cec1.wustl.edu>, abed@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