Author: Dieter Buerssner
Date: 05:44:58 11/16/00
Go up one level in this thread
On November 16, 2000 at 05:38:02, martin fierz wrote:
>i have another question about speed: i'm using large arrays for different
>things, hash-xoring, bit-counting, some arrays in the evaluation etc. i noticed
>that the final speed of my program depends on how i declare them - but the
>behavior seems rather erratic. does anybody know if there should be a difference
>between declaring globals like this:
>
>int large_array[BIG_NUMBER];
>
>or
>
>int *large_array;
>
>followed by a malloc in the initialization?
There will be produced different code by the two methods. If you have
int *large_array;
int foo = large_array[0];
first large_array must be put into an register. Assuming x86 and register edx
should be used for foo, you will get assembler like
mov eax, _large_array
mov edx, [eax]
with
int large_array[];
int foo = large_array[0];
only one instruction will be needed, because the address of large_array will
be known. Nevertheless the malloc method can be faster, because malloc allways
returns a perfectly aligned pointer. Depending on compiler options, arrays
may have a smaller alignment. This all also depends on the details of the
caching mechanisms. Say you have int array[4], that happens to be aligned
to a 8 byte aligned address, and a cache line of 16 bytes. Then two cache lines
will be needed. With and int *array, that is malloced to a 16 byte aligned
adress, one chache line will be enough.
>and when i'm using an array in the evaluation function, should there be a
>difference between declaring the array as either a global array, or
>alternatively declaring it in the function as static int array={1,2,3,...}?
I think, this shouldn't make a difference. However, I have seen some weird
performance results, depending on the memory layout of the executable.
With my AMD K6-2, there seems to be large penalty, when data is accessed,
that is very close to the code, that accesses it. I have a small example
(a pseudo random number generator), that runs 10 times slower, when the
data is too close to the code. Depending on the compiler, this can happen,
when you declare the array inside the function. Note, that this example was not
with MS Visual C, but rather with the GNU C compiler.
Regards,
Dieter
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.