Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Optimizing C code for speed

Author: Matt Taylor

Date: 19:26:09 01/01/03

Go up one level in this thread


On January 01, 2003 at 20:54:50, Bruce Moreland wrote:

>On January 01, 2003 at 19:27:21, Dieter Buerssner wrote:
>
>>On January 01, 2003 at 19:12:16, Bruce Moreland wrote:
>>
>>>>>which is faster :
>>>>>do {}  while()    or     while () {}
>>>>
>>>>compilers are very good in handling this but
>>>>in general
>>>>
>>>>do {
>>>>  ..
>>>>} while();
>>>>
>>>>is better for processors (assuming no -O2 but -O) because it
>>>>will allow fall through prediction of processors better.
>>
>>>I assume that the compiler is going to get it right.  If I need to check up on
>>>the compiler later, I do.  But the point of a high level language is that you
>>>can write for clarity, and this should be what you do by default.
>>
>>I don't agree in this specific case. With do {} while you give the compiler more
>>hints (and also the reader of the source code). You know in advance, that the
>>loop is executed at least once. It is obvious, that the first test in the
>>alternative while(test) will be omitted. Sure, most often, it will not make a
>>significant difference. But it really is different code. And, I have seen cases
>>in my chess engine, where it seems to make things (very little bit) faster.
>>Examples might be looping over a piece list. There will (in mamy engines at
>>least) allways be a K on the board. Something like while (piece_list[i]) or
>>for (i=0; piece_list[i]; i++) does not take account of this knowledge, and a
>>compiler most probably can never figure out this.
>
>He's talking about writing it taking into account branch prediction.  That's not
>the way to operate.
>
>Your example is better, because you are not just writing the same thing two
>different ways in order to try to do something the compiler will do anyway.
>
>If you know there is always a king, it makes no sense to do the "done" test on
>the first element, so you are right.
>
>But assuming that "do {} while ()" is better because of branch prediction is not
>right.  The compiler can move things around in this case if it's better.
>
>bruce

Particularly useless in this case since there is no difference in
branch-prediction between a do/while loop, for loop, or while loop.

Other (better) examples are very valid, though. Consider the ordering of cases
in a switch statement or in a series of if statements. Ordering it properly can
yield extra performance without impeding readability. This is less important in
a switch/case as the compiler will rearrange large ones to reduce the number of
comparisons. Smaller switch/case statements become equivalent to if statements,
though.

You are certainly right that the best advice is to code without regard to
low-level optimization. Later, using a profiler, you can optimize code as is
necessary. Usually if you are so desperate for performance, you want to write
that particular segment of code in assembly anyway.

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