Optimizing array accesses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clemens Lode

    Optimizing array accesses

    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

  • Ivan Vecerina

    #2
    Re: Optimizing array accesses

    "Clemens Lode" <ghoul@clawsoft ware.de> wrote in message
    news:bmbj1r$8gl $1@news.rz.uni-karlsruhe.de...[color=blue]
    > 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.[/color]
    b is const int** or const int (*)[someInt] , I assume.
    [color=blue]
    > 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?[/color]

    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
    --
    Ivan Vecerina - expert in medical devices, software - info, links, contact information, code snippets





    Comment

    • Jon Bell

      #3
      Re: Optimizing array accesses

      In article <bmbj1r$8gl$1@n ews.rz.uni-karlsruhe.de>,
      Clemens Lode <ghoul@clawsoft ware.de> wrote:[color=blue]
      >
      >Is it worth to use a[x] instead of b[y][x] (with a=&(b[y][0]) ?[/color]

      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 <jtbellap8@pres by.edu> Presbyterian College
      Dept. of Physics and Computer Science Clinton, South Carolina USA

      Comment

      Working...