Optimizing array accesses

C

Clemens Lode

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
 
I

Ivan Vecerina

Clemens Lode said:
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
 
J

Jon Bell

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top