Pointer and Array

R

Raman

Is there any performance gain in using


*(a+i) instead of a

for declaration
int a[10]

If no then why people call pointers more faster than arrays.

if yes, how ?

Thanks
RamanDeep
 
V

Vladimir S. Oka

Raman said:
Is there any performance gain in using


*(a+i) instead of a


Any decent optimising compiler should generate exactly the same code for
both, so no, I don't think there's any difference (as a funny aside,
i[a] should also be exactly the same).
for declaration
int a[10]

If no then why people call pointers more faster than arrays.

First time I heard it...
 
K

Keith Thompson

Raman said:
Is there any performance gain in using


*(a+i) instead of a

for declaration
int a[10]


Probably not, since a[10] is defined to be equivalent to *(a+10). But
this equivalence means that they behave the same way, not necessarily
that they're implemented using the same machine code.
If no then why people call pointers more faster than arrays.

The language standard says nothing about the relative performance of
various conststructs. There are cases where using pointers *might* be
quicker than the equivalent code using array indexing.

For example, consider this:

#include <stdio.h>

static int using_pointer(char *s)
{
int result = 0;
while (*s != '\0') {
result += *s;
s ++;
}
return result;
}

static int using_index(char *s)
{
int result = 0;
int i = 0;
while (s != '\0') {
result += s;
i ++;
}
return result;
}

int main(void)
{
char *s = "hello, world";
printf("using_pointer(s) returns %d\n", using_pointer(s));
printf("using_index(s) returns %d\n", using_index(s));
return 0;
}

Both functions compute the sum of the character values of the string
argument (not a terribly useful thing to compute, but it illustrates
the point). For a sufficiently naive compiler, using_pointer() might
be more efficient than using_index(), because using_pointer steps
through the string by incrementing a pointer, while using_index
increments and index *and* recomputes the location of the array
element.

On the other hand, if array indexing is done by some kind of
addressing mode supported directly in hardware, the indexing form
*might* be faster than the pointer form.

But modern compilers are quite clever about this kind of optimization.
If the pointer form is more efficient than the index form, the
compiler is likely to be able to recognize what you're doing and
translate the index form into the pointer form -- and vice versa.

Write code for clarity. If the result isn't fast enough, *measure*
the performance and consider tweaking the code to make it faster, but
don't be surprised if the compiler has already performed optimizations
that you would never have thought of.
 
F

Flash Gordon

Raman said:
Is there any performance gain in using


*(a+i) instead of a

for declaration
int a[10]


Almost certainly not.
If no then why people call pointers more faster than arrays.

Because they are over a decade out of date.

Write the code to be clear and let the optimiser worry about optimising it.
 
J

John Tsiombikas (Nuclear / Mindlapse)

Because they are over a decade out of date.

Write the code to be clear and let the optimiser worry about optimising it.

Solid advice, also let me add: When you decide to optimize (after
determining where it is needed by measuring), do not try to outsmart the
compiler, most of the time you are making the optimizer's job harder.
Just take a step back and reconsider your algorithms from a higher
level.
 

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
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top