int x[5];
x[4] = 10;
*(x + 4) = 10; Array indexing has a cleaner syntax but (as I've read) pointer arithmetic is faster. My question is, is it worth to use pointer arithmetic? Is there a noticeable difference?
int x[5];
x[4] = 10;
*(x + 4) = 10; Array indexing has a cleaner syntax but (as I've read) pointer arithmetic is faster. My question is, is it worth to use pointer arithmetic? Is there a noticeable difference?
Short answer: for the tiny example that started this thread (), not worth it. With optimizations enabled most compilers generate essentially the same code for array indexing and the equivalent pointer expression. 's practical point stands: prefer the clearer form so the code is easy to read and maintain.
When differences show up they are usually narrow cases: very tight inner loops, old or constrained embedded toolchains, or builds with bounds checking / sanitizers enabled. Pointer-walking can let a loop avoid recomputing an index each iteration, but modern optimizers typically turn both loop styles into the same assembly. Other reasons you might see a difference include aliasing rules (strict-aliasing) or undefined-behavior from incorrect pointer casts. Also note that container operator[] does not do bounds checking (use at() if you need checks). and are right that loop shape and target constraints matter — on some embedded targets you may need every saved cycle.
Practical workflow: write clear code first (range-based for, iterators, std::algorithms or std::span), profile to find real hotspots, then optimize. If you must micro-tune, compile with optimization flags, inspect the generated assembly (Compiler Explorer or an objdump), and measure on the target CPU before committing pointer-based changes. Only optimize where profiling proves it helps.
Jump to Post— ddanbe 2,724Array indexing has a cleaner syntax
You said it yourself! So why bother and make your life difficult. When you come back later to your code it will be more easy to understand then if you used pointer arithmetic. If you have a good C++ compiler he would translate both …
Array indexing has a cleaner syntax
You said it yourself! So why bother and make your life difficult. When you come back later to your code it will be more easy to understand then if you used pointer arithmetic. If you have a good C++ compiler he would translate both options the same way!
Alright, thanks! :)
>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:
int x[N];
int *p = x;
for ( int i = 0; i < N; i++ )
process ( x[i] );
for ( int i = 0; i < N; p += i )
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.
From the C++ Standard:
The expression E1[E2] is identical (by definition) to *((E1)+(E2)).
Sometime pointer arithmetics code is evidently cleaner than "subsripted" one:
int countDigits(const char* str)
{
int n = 0;
if (str)
while (int c = *str++)
if (c >= '0' && c <= '9')
++n;
return n;
} Sometimes we have no choice: as usually (but not always) pointer arithmetics code is faster than "subscripted" one on embedded systems where we (often) must save on every microsecond...
It's a far-fetched problem. It's not a language problem...
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.