Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Optimizing C code for speed

Author: Matt Taylor

Date: 13:34:02 01/02/03

Go up one level in this thread


On January 02, 2003 at 07:22:30, Andreas Guettinger wrote:

>On January 01, 2003 at 21:55:10, Russell Reagan wrote:
>
>>On January 01, 2003 at 19:13:20, Uri Blass wrote:
>>
>>>1)why it should not matter even with no optimization?
>>>
>>>Correct me if I am wrong but it seems that with the fastest way you need more
>>>memory because you need to remember also x so theoretically it is possible that
>>>the computer will crush because of memory problem in the first case when it does
>>>not crash in the second case.
>>
>>It will be in a register if it's a double word sized value. If not it will be in
>>the cache. Memory has nothing to do with this problem.
>>
>>>2)Suppose that you try optimization for minimize size and not maximal speed
>>>Am I correct to assume that the compiler will not define the varaible x?
>>
>>If the data is used often, then it will be in the cache and there will be no
>>memory accessed at all. If it is not used often, then it is not a critical part
>>of the program most likely (in terms of speed). Besides, the cpu doesn't think
>>in terms of "variables". The compiler won't have anything to do with whether or
>>not this value is in the cache. That's a hardware thing, not a software thing.
>
>
>Ok, thanks.
>So for another example, when I refer to a integer in a struct several times,
>i.e.
>
>if (movelist.move[i].flag == 3)
>
>say A) 1-5 times, B) 5-10 times, C) >10 times (i.e. in a loop)
>
>then should I put it in a local variable first (for cases A, B, C), so the
>compiler keeps it in the cash, or will the compiler keep it in the cash anyway?
>
>(register) int loflag = movelist.move[i].flag
>
>(do { )
>   if (loflag == 3)
>   ....
>(while()} )
>
>regards Andy

It is not necessary. It is merely a hint to the compiler, "Hey, you should put
this in a register." The compiler doesn't have to take the hint, but I find that
most compilers, even with simple optimization, will naturally take the hint. I
think the register keyword is also unnecessary; usually the compiler can pick
out which variables belong in registers. It will even suprise you and do better
than you expected.

You should be careful not to go overboard with these sorts of tricks. If you
constantly move things into local variables, your code will be more difficult to
read, and it probably won't achieve any real gain. The compiler can recognize
many optimizations that are difficult for people to see, particularly when it
comes to the quirks of an instruction set. In some cases, it could be
counterproductive to try and get the compiler to generate code in a specific
fashion.

Consider a snippet of code which does integer vector addition (similar to
example pointed out earlier):

int add_them(int *dest, int *src1, int *src2, int len)
{
    int i;

    for(i = 0; i < len; i++)
        *dest++ = *src1++ + *src2++;
}

This could be more efficient on x86 (and much more readable IMO) written as
follows:

int add_them(int *dest, int *src1, int *src2, int len)
{
    int i;

    for(i = 0; i < len; i++)
        dest[i] = src1[i] + src2[i];
}

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