Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: C optimization: int vs char

Author: Bruce Moreland

Date: 09:09:15 10/10/01

Go up one level in this thread


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?

If you have a function that is diddling with a 256-element array, and the index
is a local variable, using an int has little to do with cache.  If the function
is simple, the value is going to go into a register.  If it's more complicated,
and the value can't fit in a register, there are probably more important
performance issues elsewhere.

Even if it made some sense for the compiler to try to use a byte for the index,
at some point the compiler is going to have to stick the byte into a 32-bit
register.  There is no instruction that says:

   mov eax, [bl]

Instead, you get:

   mov eax, [ebx]

The difference between the two is that the first one tries to use the lower 8
bits of ebx, and the second one uses the whole thing.  The first one doesn't
exist.

So in order to get to the second one, the compiler has to make sure that the top
bits are clear on every iteration of the loop.

It's likely in this case that the compiler could avoid adding extra code in each
iteration in order to having something in the top bits, but it's not like
there's any up-side, so why make it worry about it?

bruce

>
>Thanks
>
>Travers



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.