Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Optimizing C code for speed

Author: Robert Hyatt

Date: 19:07:07 01/02/03

Go up one level in this thread


On January 01, 2003 at 11:50:58, Lieven Clarisse wrote:

>I was wondering if there is a good book about how to write efficient C code. I
>am not talking about algorithms, but the way *how* to write things, eg
>
>which is faster :
>do {}  while()    or     while () {}

What is required in the program?  The two statements are _not_ identical.

The first one has a single jump at the bottom.  The second has a jump at the
top to skip the loop if the condition is initially false, and a jump at the
bottom that is always taken, although it could be optimized so that you put
the test outside the loop and make both look _exactly_ the same after the
initial test for the while has been done...

It's simply not important enough to worry about.


>
>-------
>I know for instance that:
>
>ptr=&R[i];
>if((*ptr==3)||(*ptr==7)) {;}
>
>is faster then:
>
>if((R[i]==3)||(R[i]==7)) {;}


I don't see any reason why that would be true.  When the C compiler sees
r[i] it simply references *(r+i) anyway.  If I is already handy in a register,
there is a clever trick in the CPUs addressing that will scale I, and add it to
R with no penalty in extra instructions, and I is _still_ in a register for
use on other things.



>
>Is there a good book that reviews all those kinds of tricks?
>
>regards,
>
>Lieven



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.