Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Optimizing C code for speed

Author: Bruce Moreland

Date: 10:13:53 01/02/03

Go up one level in this thread


On January 02, 2003 at 09:49:09, Graham Laight wrote:

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

The compiler could just look at the above, decide that you are crazy, and do it
however it wants.

I listened to a lecture by the Microsoft compiler guys, circa 1989, and they
were talking about being able to optimize "s[i]" based code to "*pb" based code
even then.

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

Assuming it matters at all.

I kid you not, I have watched people try to speed optimize code that executes
once and then brings up a dialog box.  If you are trying to save three
instructions on code that is going to bring up a dialog box, you are crazy.

bruce

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