array performance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Pool

    #1

    array performance

    Hi,

    I have on large threedimensiona l 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

    #2
    Re: array performance


    "Frank Pool" <Frank.Pool@gmx .net> wrote[color=blue]
    > I have on large threedimensiona l 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];
    >[/color]
    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.[color=blue]
    >
    > Is there a performance difference between retriveing many elements of the
    > small and the large array in my function ?
    >[/color]
    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.


    Comment

    • Barry Schwarz

      #3
      Re: array performance

      On Sun, 30 Jan 2005 16:05:04 +0100, "Frank Pool" <Frank.Pool@gmx .net>
      wrote:
      [color=blue]
      >Hi,
      >
      >I have on large threedimensiona l array
      > int largeArray[1024][64][4];[/color]

      largeArray is an array of 1024 arrays of 64 arrays of 4 int.
      [color=blue]
      >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];[/color]

      smallArray is a pointer to an array of 64 arrays of 4 int.
      [color=blue]
      >smallArray = &largeArray[42];[/color]

      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.
      [color=blue]
      >
      >Is the following code correct if I want to get the value of an element ?
      >int x = smallArray[0][0];[/color]

      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.
      [color=blue]
      >
      >Is there a performance difference between retriveing many elements of the
      >small and the large array in my function ?
      >[/color]
      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>>

      Comment

      Working...