Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Optimizing C code for speed

Author: Graham Laight

Date: 06:49:09 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 () {}
>
>-------
>I know for instance that:
>
>ptr=&R[i];
>if((*ptr==3)||(*ptr==7)) {;}

In terms of number of machine code steps required to execute the program,
wouldn't the above be the same as:

x = R[i];
if ((x == 3) || (x == 7))  ?

>is faster then:
>
>if((R[i]==3)||(R[i]==7)) {;}

I would be sceptical of this claim without testing:

* the first option still requires the retrieval of R(I)

* there's an extra line of code to execute (the pointer assignment)

One way to find out without having to study the compiled code is to simply write
both options into a program in a big loop (a million repetitions), and have the
program time how long each of the two loops will take.

-g

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