Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Optimizing C code for speed

Author: Robert Hyatt

Date: 07:26:59 01/03/03

Go up one level in this thread


On January 03, 2003 at 03:16:00, Matt Taylor wrote:

>On January 02, 2003 at 22:21:10, Robert Hyatt 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) {;}
>>
>>
>>So far as I know, at least GCC and Intel's compiler do this already.  gcc
>>considers R[i] as a "very busy expression" and keeps it in a register once
>>which would carry the optimization across multiple statements.  In the above,
>>any good compiler should recognize and not even do what you suggest.  It should
>>simply recognize from the dependency graph that R[i] is used twice and is not
>>changed between uses so a second load is not needed...
>
>This is does, but I was trying to avoid lengthy discussion about it because it
>is unimportant. I corrected the statement in another post and said that it is
>"not necessarily faster." It is merely a hint to the compiler that it should
>cache R[i] in a register. Compilers usually take the hint unless they are
>generating debug code. In debug code, it really wouldn't matter, and in
>particular in VC, you might even lose more than you gain.
>
>Also FYI it is not universally true that you can't view the contents of a
>variable that gets optimized off of the stack. That is not to say that you can
>cheat and look at the register yourself; it is to say that certain debuggers are
>smart enough to know where a variable is and when it is.
>
>-Matt


If it isn't in memory, it might not exist, which was the point...




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.