3 ms·
In which language? In Rust iterators are actually the fastest way to safely iterate over arrays, because they will elide bounds checks. If you use `array[index
by Matthias247 5y ago
In which language?
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.
- dnautics 5y agoZiglang. 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.