> Compiler and hardware technology have advanced to the point where a loop using array indices can be as efficient as a loop using pointer arithmetic.
I literally refactored some code from index arithmetic to pointer arithmetic and bought a 10% increase in performance for an (admittedly silly) numerical performance contest on Friday, so I'm not convinced either LLVM or the "jit compiler that reorders operations inside the x86 chip" are that smart yet.
That said I would not doubt that most modern languages convert iterables that look like index arithmetic into pointer arithmetic, but if they do so I would suspect it's at an IR level above the general compiler backend.
In Rust iterators are actually the fastest way to safely iterate over arrays, because they will elide bounds checks. If you use `array[index]` you will get a mandatory bounds check (branch) on each access. Using pointer arithmetic would avoid that, but is unsafe in Rust for obvious reasons.
In C I would assume indexes and pointer arithmetic to have exactly the same performance, since the `array[i]` is the same as `*(array + i)` and there are no mandatry bounds checks. Might be interesting to move your code in godbolt and see what actually changes here.
Ziglang. We looked at it Tracy so we confirmed that having a counter and a pointer is bad relative to having just a pointer. We don't want bounds checks because they are 1) expensive (remember, silly numerical computation challenge) and 2) we overshoot the end of the array anyways to be able to aggressively unroll as many writes as possible at compile time, don't worry, we make sure to allocate extra slots that are mathematically guaranteed to be sufficient beforehand.
I literally refactored some code from index arithmetic to pointer arithmetic and bought a 10% increase in performance for an (admittedly silly) numerical performance contest on Friday, so I'm not convinced either LLVM or the "jit compiler that reorders operations inside the x86 chip" are that smart yet.
That said I would not doubt that most modern languages convert iterables that look like index arithmetic into pointer arithmetic, but if they do so I would suspect it's at an IR level above the general compiler backend.