Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Optimizing C code for speed

Author: Matt Taylor

Date: 19:01:28 01/01/03

Go up one level in this thread


On January 01, 2003 at 18:02:35, Andreas Guettinger wrote:

>On January 01, 2003 at 13:25:30, Matt Taylor 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)) {;}
>>>
>>>is faster then:
>>>
>>>if((R[i]==3)||(R[i]==7)) {;}
>>>
>>>Is there a good book that reviews all those kinds of tricks?
>>>
>>>regards,
>>>
>>>Lieven
>>
>>It varies per compiler. There is an even faster method for your second case:
>>
>>x = R[i];
>>if (x == 3 || x == 7) {;}
>>
>>The only way to know is to look at compiler output. I know for VC, the compiler
>>I use most often, it chokes on 64-bit manipulations, stack alignment > 4 bytes,
>>and small structures. It usually generates nice code with conditionals such as:
>>
>>int is_below(int x, int y)
>>{
>>    return (x < y ? 1 : 0);
>>}
>>
>>-Matt
>
>
>Why is
>
>int x = R[i];
>if (x == 3 || x == 7) {;}
>
>faster than
>
>if((R[i]==3)||(R[i]==7)) {;}
>
>Isn't the creation and assignment of x a waste of time?
>
>regards
>Andy

To answer your question as well as the replies made, I should correct myself and
say that it is not necessarily faster, but it can be faster.

Russell - The index operation itself is free in a lot of cases on x86. However,
the memory operation is not.

Uri - In most cases the compiler will recognize that x can be kept exclusively
in a register. It might require an extra register, but using an extra register
is cheaper than executing two loads.

A good compiler will recognize that you are accessing R[i] twice and it will
reduce it to a single load operation. However, using the temporary variable x
here encourages the compiler to do the load once. One can give another hint by
declaring x to be constant.

Other source-level optimizations can sometimes be made by mathematically
rearranging terms. The ternary conditional operator works well in VC; I have not
studied GCC's output on x86 with that construct. Also, switch/case statements
can be very nicely optimized -- don't be afraid to use them.

-Matt



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.