array performance

Discussion in 'C Programming' started by Frank Pool, Jan 30, 2005.

  1. Frank Pool

    Frank Pool Guest

    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
     
    Frank Pool, Jan 30, 2005
    #1
    1. Advertising

  2. Frank Pool

    Malcolm Guest

    "Frank Pool" <> 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];

    However I have been programming in C for years, and had to think about this.
    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
    itself, but it might help you organise the code efficently.
     
    Malcolm, Jan 30, 2005
    #2
    1. Advertising

  3. On Sun, 30 Jan 2005 16:05:04 +0100, "Frank Pool" <>
    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[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>>
     
    Barry Schwarz, Jan 31, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jm
    Replies:
    1
    Views:
    512
    alien2_51
    Dec 12, 2003
  2. Maxim Khitrov

    Strange array.array performance

    Maxim Khitrov, Feb 19, 2009, in forum: Python
    Replies:
    0
    Views:
    201
    Maxim Khitrov
    Feb 19, 2009
  3. Maxim Khitrov

    Re: Strange array.array performance

    Maxim Khitrov, Feb 19, 2009, in forum: Python
    Replies:
    5
    Views:
    376
  4. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    496
    David RF
    Sep 3, 2009
  5. Software Engineer
    Replies:
    0
    Views:
    335
    Software Engineer
    Jun 10, 2011
Loading...

Share This Page