0

I’m currently looking on how to optimise my C code. I found this page which says, in section 10, that pointer address indexing for the for loop would be faster than doing it with the non pointer arithmetic syntax. my question is why and if that’s even still true with all the modern compiler optimisations

If you access members of array like this:

for(int i=0; i<n; i++) nArray[i]=nSomeValue;
Instead of the above code, the following is better:

for(int* ptrInt = nArray; ptrInt< nArray+n; ptrInt++) *ptrInt=nSomeValue;
  • Don't link images: https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557 – William Pursell Dec 09 '21 at 14:47
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to [edit] your questions. For example to show examples of the "pointer arithmetic" loop. (PS. Links to external sites can go bad, always make questions self-contained.) – Some programmer dude Dec 09 '21 at 14:47
  • 4
    This smells like snake oil. – Sinan Ünür Dec 09 '21 at 14:48
  • As for pointer arithmetic, for any pointer *or array* `p` and index `i`, the expression `p[i]` is *exactly* equal to `*(p + i)`. So any "normal" array access and indexing is still being a form of pointer arithmetic. – Some programmer dude Dec 09 '21 at 14:48
  • https://stackoverflow.com/questions/2305770/efficiency-arrays-vs-pointers – moooeeeep Dec 09 '21 at 14:48
  • That’s exactly what I thought, why should it be faster it is pretty much exactly the same, function wise – paul heiner Dec 09 '21 at 14:51
  • 1
    This may have been partly true a long time ago with simple non optimizing compilers. Today with modern compilers, both methods will most likely result in the same code. – Jabberwocky Dec 09 '21 at 14:53
  • Okay thanks for the confirmation, I’m not too trusted with C optimising yet… – paul heiner Dec 09 '21 at 14:54
  • The result is the same for both versions: https://godbolt.org/z/x7f54vjoz The code at the beginning of the functions are a little bit different, but the loop is the same. – mch Dec 09 '21 at 14:55
  • 1
    The worst you could do is premature optimization. Optimize **only** if your code is too slow. Otherwise let it be readable at the maximum level. Write what you intend, not what you think the compiler should be forced into. -- And even if you optimize, such minor gains as shown in the (in parts plain wrong) page you link, refactor, and use a better algorithm. If that's not possible, you can only save corner cases. You will never make an Arduino do complex video manipulation, for example. – the busybee Dec 09 '21 at 14:56
  • Yeah absolutely but I’m optimising for a few low-level mathematical operations so I guess it makes sense for then. – paul heiner Dec 09 '21 at 15:00
  • If I’m already here, another very small question. Does it make a difference if I return a “return value” via a pointer in the parameters or via a return statement if I am gonna allocate the memory for the return anyway? – paul heiner Dec 09 '21 at 15:01
  • I can confirm by experience that, 35 years ago, such tricks really improved efficiency of the code. 35 years ago. – Damien Dec 09 '21 at 15:02
  • Okay, I’m only 19 so I don’t have that much experience. sorry – paul heiner Dec 09 '21 at 15:32
  • But I’m really grateful for your wisdom :) – paul heiner Dec 09 '21 at 15:43

0 Answers0