Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: C optimization: int vs char

Author: Frank Phillips

Date: 11:18:34 10/10/01

Go up one level in this thread


On October 10, 2001 at 10:04:18, Robert Hyatt wrote:

>On October 10, 2001 at 08:38:43, Travers Waker wrote:
>
>>Hi.
>>
>>I recently read that using word-sized variables for a particular machine (e.g. a
>>32-bit int on an Intel PC CPU) yields faster code than using variables that are
>>not the size of a word.
>>
>>For example, I understood the article to be saying that if you needed an index,
>>i,  to access an array, A[256], then it would yield faster code to define:
>>
>>int i;
>>
>>instead of:
>>
>>unsigned char i;
>>
>>even though the unsigend char is big enough to be able to index all the elements
>>of the array.  I find this strange, especially since it seems to me that you
>>waste space in the memory caches when the most sinificant 3 bytes of the 32-bit
>>int are always going to be zero's.
>>
>>Can someone explain to me why using word-sized variables should be faster?
>>
>>Thanks
>>
>>Travers
>
>
>It is faster _inside_ the cpu.  But chars are generally faster _overall_ if you
>have arrays of them.  Or lots of them.  This becomes a trade-off.  You use
>instructions that are a bit slower inside the CPU, but the instructions and
>data stick in L1/L2 cache and the speed advantage of avoiding memory references
>more than offsets the somewhat slower instructions.
>
>In the PC, the memory system is the biggest bottleneck you have to overcome.
>Even if it means using instructions that are a bit slower.
>
>The difference between 8 ints and 32 chars in a single line of cache is very
>significant.

Kind of related.

My program runs slower on gcc3.0.1 than it does on gcc2.96 than it does on
MSVC++.
The executable is biggest with gcc3.0.1 (around 490k), smaller with 2.96 and
smallest with MSVC++.

I suspect a causal relationship, although not a full explanation of the
differences.

When I find out how to access the gcc3.0.1 documentation I installed I may try
to reduce the compiled size and see if it gets faster.

(I would not like to speculate how much of the code is actually making it play
better chess, but that is a whole different subject.)

Frank



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.