Author: Matt Taylor
Date: 16:21:08 01/03/03
Go up one level in this thread
On January 03, 2003 at 07:52:46, 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)) {;}
>>
>>is faster then:
>>
>>if((R[i]==3)||(R[i]==7)) {;}
>>
>>Is there a good book that reviews all those kinds of tricks?
>>
>>regards,
>>
>>Lieven
>
>Forgetting the pointer and saving to a variable DOES save time - and now I can
>prove it!
>
>Below is a web page I have written, with the button actions written in
>JavaScript.
<snip>
JavaScript is not optimized, and it's interpreted. C is the opposite. Reducing
the number of array accesses will probably save a lot of interpret time as it
won't have to do as much checking. Doing the same in C will usually just make
the compiler spend a couple extra microseconds doing the conversion for you.
In C, most compilers will do this for you. They can get a little confused in
complex functions because they are looking to save things most frequently used
in registers. Usually a memory load like this is pretty insignificant anyway. It
will only cost you 2-3 additional cycles if not in a register, and if the
compiler can overlap other operations, you won't lose anything.
-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.