Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Optimizing C code for speed

Author: Dave Gomboc

Date: 05:47:23 01/02/03

Go up one level in this thread


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

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

That isn't necessarily true.  It depends on the instruction set (and the
compiler, of course).

I'd write this as:

const int temp = R[i];
if ((3 != temp) || (7 != temp)) { ... };

That way, a dumb compiler will still do the memory access only once, in the most
natural way for the native instruction set.

(I don't put the ints first for optimization, I put the ints first so if I
mistype (3 == temp) as (3 = temp) I get a compile-time error. :-)

Dave



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.