Optimizing array accesses

Discussion in 'C++' started by Clemens Lode, Oct 12, 2003.

  1. Clemens Lode

    Clemens Lode Guest

    Hello!

    Is it worth to use a[x] instead of b[y][x] (with a=&(b[y][0]) ?
    a is const int*, b is const int.
    y is a static int and does only change once (at the beginning of the
    program).

    That would be one less memory access and thus faster, right?

    Thanks :)
    Clemens
    Clemens Lode, Oct 12, 2003
    #1
    1. Advertising

  2. "Clemens Lode" <> wrote in message
    news:bmbj1r$8gl$-karlsruhe.de...
    > Is it worth to use a[x] instead of b[y][x] (with a=&(b[y][0]) ?
    > a is const int*, b is const int.

    b is const int** or const int (*)[someInt] , I assume.

    > y is a static int and does only change once (at the beginning of the
    > program).
    >
    > That would be one less memory access and thus faster, right?


    It's hard for us to tell if it is "worth" it.
    But yes, it will usually be faster to pre-calculate the
    address of the sub-array that you will work with.
    So it is a good idea, especially if variable 'a' is
    given a meaningful name...

    Regards,
    Ivan
    --
    http://ivan.vecerina.com
    Ivan Vecerina, Oct 12, 2003
    #2
    1. Advertising

  3. Clemens Lode

    Jon Bell Guest

    In article <bmbj1r$8gl$-karlsruhe.de>,
    Clemens Lode <> wrote:
    >
    >Is it worth to use a[x] instead of b[y][x] (with a=&(b[y][0]) ?


    Maybe. Maybe not. It depends on the quality of the optimizer in your
    particular compiler, and on your particular application.

    Check the code generated by your compiler to see what it does under
    various levels of optimization. It may already do this particular trick
    for you. If not, test both versions of the code and see it it actually
    makes a significant difference in speed.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Oct 12, 2003
    #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. Gary
    Replies:
    0
    Views:
    827
  2. A.M-SG
    Replies:
    4
    Views:
    414
    Steven Cheng[MSFT]
    Nov 2, 2005
  3. Replies:
    1
    Views:
    336
    Furious George
    Apr 19, 2006
  4. Adi

    memory accesses

    Adi, Mar 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    321
    Mike Wahler
    Mar 5, 2004
  5. John Regehr

    systematic miscompilation of volatile accesses

    John Regehr, Apr 29, 2008, in forum: C Programming
    Replies:
    0
    Views:
    469
    John Regehr
    Apr 29, 2008
Loading...

Share This Page