Which is faster way to use an element pointer rather than an index when scanning arrays? need some clarification why?

Recommended Answers

All 5 Replies

The difference is sooo small that it doesn't really matter. If there is any difference at all it's probably measured in just a few nanoseconds. And a good compiler optimizer will most likely make them both the same. What you should be asking is which is simpler to read, which is more maintainable for us humans.

@Ancient Dragon
Thank you for answer, so what should I consider there is no difference in processing array between by pointer and by index

There is generally no difference between using an index or using a pointer. Using pointers is technically faster, but using an index will usually result in the same code once the compiler has optimized it (unless it is really bad at optimizing or that the context makes the preservation of the index value necessary).

So, the idea here is that you should use whatever feels more natural and clear in the context. For example, if I want to fill an array with sequence numbers, I would do this:

int number_seq[100];
for(int i = 0; i < 100; ++i)
  number_seq[i] = i;

Instead of this:

int number_seq[100];
for(int* p = number_seq; p != number_seq + 100; ++p)
  *p = (p - number_seq);

Because the first one is much clearer and more natural. The resulting compiled code is likely to be identical in both cases.

In other words, this is just one of these cases where you can trust the compiler to produce pretty much the best code possible, and all you have to worry about is making it "human-friendly".

Also, in C++, there is strong emphasis on the concept of "iterators", which are a kind of abstract generalization of pointers in the context of traversing a sequence of values (array, list, etc.). And in that sense, using pointers to traverse the array is a "natural" thing to do in C++, but obviously, index-based traversals are preferred in many contexts similar to the example above (and for example, when you need to traverse multiple arrays by using the same index into them).

@Mike
Thank you Mike, is there any differenece in accessing mechanism of array element by pointer and index. As you mention in your post

Using pointers is technically faster

What is the reason I like to know that

If you consider the loop that I posted last. The index version would, without any optimization, turn into this:

int i = 0;
while( i < 100 ) {
  int* temp_ptr = number_seq + i;    // | equivalent to:
  *temp_ptr = i;                     // | number_seq[i] = i;
  i += 1;      // or ++i
};

While the pointer version turns into:

int* p = number_seq;
while( p != number_seq + 100 ) {
  *p = (p - number_seq);
  p += 1;    // or ++p
};

Essentially, the index version adds one line of code, which is to compute the pointer to the element you are accessing before dereferencing that pointer. But most compilers will be able to see that the two lines (increment index, and add index to start-pointer) can be replace by just incrementing the pointer, which is why it ends up performing the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.