Velocity Reviews > array performance

array performance

Frank Pool
Guest
Posts: n/a

 01-30-2005
Hi,

I have on large threedimensional array
int largeArray[1024][64][4];
In a particular function I only need a part of this array
So I'm using a new variable and assign it the follwoing way:

int (*smallArray)[64][4];
smallArray = &largeArray[42];

Is the following code correct if I want to get the value of an element ?
int x = smallArray[0][0];

Is there a performance difference between retriveing many elements of the
small and the large array in my function ?

Thanks,
Frank

Malcolm
Guest
Posts: n/a

 01-30-2005

"Frank Pool" <(E-Mail Removed)> wrote
> I have on large threedimensional array
> int largeArray[1024][64][4];
> In a particular function I only need a part of this array
> So I'm using a new variable and assign it the follwoing way:
>
> int (*smallArray)[64][4];
> smallArray = &largeArray[42];
>
> Is the following code correct if I want to get the value of an element ?
> int x = smallArray[0][0];
>

I don't think so. smallArray is a pointer to a list of int [64][4] s. So
smallArray[0] would be the first 2d array (&largeArray[42]), smallArray[1]
would be the next (&largeArray[43]) and so on. So smallArray[0][0] actually
returns a list of 4 integers, and you need (*smallArray)[0][0];

This is the problem with mutli-dimensional arrays. Beyond the very basics,
the syntax is horrible and confusing and even an experienced programmer will
find it difficult to understand your program.
>
> Is there a performance difference between retriveing many elements of the
> small and the large array in my function ?
>

Depends on the platform. Your huge array probably won't fit in a cache, but
if you can arrange matters so that acesses that are continguous in memory
are also close together in order, you might see some speed-up. Declaring a
pointer to a small part of the array is unlikely to have an impact by

Barry Schwarz
Guest
Posts: n/a

 01-31-2005
On Sun, 30 Jan 2005 16:05:04 +0100, "Frank Pool" <(E-Mail Removed)>
wrote:

>Hi,
>
>I have on large threedimensional array
> int largeArray[1024][64][4];

largeArray is an array of 1024 arrays of 64 arrays of 4 int.

>In a particular function I only need a part of this array
>So I'm using a new variable and assign it the follwoing way:
>
>int (*smallArray)[64][4];

smallArray is a pointer to an array of 64 arrays of 4 int.

>smallArray = &largeArray[42];

largeArray[42] is the n-th element (out of the possible 1024 elements)
of largeArray. this element is an array of 64 arrays of 4 int.
&largeArray[42] is the address of this element with the same type as
smallArray.

>
>Is the following code correct if I want to get the value of an element ?
>int x = smallArray[0][0];

No. smallArray is a pointer to the first of a series of objects, each
of which is an array of 64 arrays of 4 int. smallArray[0] is the n-th
such object pointed to and is therefore an array of 64 arrays of 4
int. smallArray[0][0] is the n-th array (of the possible 64 arrays)
and has type array of 4 int.

As you have set it up, you need to use (*smallArray)[0][0].

Alternatively, you could consider the following:

largeArray[42] is a particularly object (out of the 1024 that
largeArray consists of) with type 64 arrays of 4 int.

In most contexts, the expression largeArray[42] evaluates to the
address of the first element, that is the address of the first array
of 4 int (out of the possible 64) with type pointer to array of 4 int.
One exception to this rule (there are others) is when the expression
is the operand of the & operators, as it is in your original code.

If you code
int (*smallArray)[4];
you can assign to it with
smallArray = largeArray[42];
and then refer to individual integers with smallArray[i][j] which I
find to be a much easier notation to read and to type.

>
>Is there a performance difference between retriveing many elements of the
>small and the large array in my function ?
>

A quality of implementation issue which is not part of the language we
discuss here. You can ask in a newsgroup where your compiler is
discussed.

<<Remove the del for email>>