View Single Post
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: array indexing vs pointer arithmetic

 
0
  #4
Nov 9th, 2008
>but (as I've read) pointer arithmetic is faster
Prove it. In your example, the two are quite likely compiled to exactly the same machine code. Array subscripting is literally syntactic sugar for the equivalent pointer offset calculation. A more likely example is this:
  1. int x[N];
  2. int *p = x;
  3.  
  4. for ( int i = 0; i < N; i++ )
  5. process ( x[i] );
  6.  
  7. for ( int i = 0; i < N; p += i )
  8. process ( *p );
The latter could very well be faster (though I would question why you're worried about such micro-optimizations) due to the fact that the body of the loop is only performing a dereference rather than an addition and a dereference.

My advice is not to worry about it. Use whichever more clearly displays your intentions.
Last edited by Narue; Nov 9th, 2008 at 10:32 am.
I'm here to prove you wrong.
Reply With Quote