Referencing array contents performance

P

pinkisntwell

Is it more efficient to reference array contents through pointer
arithmetic than it is to use the standard referencing with the
brackets? For example, say I have this code:

int arr[100];
arr[5];
*(arr+5);

Are there any performance gains in using the pointer arithmetic way of
accessing?
 
J

Jonathan Lee

Are there any performance gains in using the pointer arithmetic way of
accessing?

In principle, I don't think so. That is, I doubt the standard says
anywhere that one should be faster. In practice, I bet if you compile
and disassemble both versions you'll get exactly the same code.

--Jonathan
 
J

James Kanze

Is it more efficient to reference array contents through pointer
arithmetic than it is to use the standard referencing with the
brackets? For example, say I have this code:
int arr[100];
arr[5];
*(arr+5);

By definition, arr[5] *is* *(arr+5). If there's a difference, it can
only be because the compiler writer wanted to punish you for using one
or the other.
Are there any performance gains in using the pointer arithmetic way of
accessing?

Why? Does your program perform too slowly using one, and not using
the
other?
 
D

Daniel Pitts

Jonathan said:
In principle, I don't think so. That is, I doubt the standard says
anywhere that one should be faster. In practice, I bet if you compile
and disassemble both versions you'll get exactly the same code.

--Jonathan
Although, while iterating over the contents, you *might* find a
performance difference:

given: T myArray[];

for (int i = 0; i < 10; ++i) {
myArray = makeT();
}

vs.

for (T *it = myArray, end = myArray + 10; it < end; ++it) {
*it = makeT();
}

Although, which one is faster probably depends on a lot of factors.
Also, I would imagine that a smart enough compiler would translate the
former to the latter, if it felt there was a good enough reason too.
 
B

Balog Pal

pinkisntwell said:
Is it more efficient to reference array contents through pointer
arithmetic than it is to use the standard referencing with the
brackets? For example, say I have this code:

int arr[100];
arr[5];
*(arr+5);

The last two are equivalent. Just as
5[arr];

the standard defines [] that very way.
Are there any performance gains in using the pointer arithmetic way of
accessing?

Only with a seriously broken compiler.
 
K

Kenshin

Is it more efficient to reference array contents through pointer
arithmetic than it is to use the standard referencing with the
brackets? For example, say I have this code:

int arr[100];
arr[5];
*(arr+5);

Are there any performance gains in using the pointer arithmetic way of
accessing?

And after all the correct & wise ansers above (equivalent peformance &
code generated), do use [] notation for clarity :)
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top