This recommendation is part of the open catalog of best practice rules for performance that is automatically detected and reported by Codee.
Issue
Strided array access may impact performance.
Relevance
Accessing an array using a non-unit stride is less efficient than accessing consecutive positions because the latter improves the locality of reference. Note that C/C++ column-wise matrix access is an example non-unit stride, where the stride is the column width.
Actions
Consider using techniques like loop fusion, loop interchange, loop tiling or changing the data layout to avoid non-sequential access in hot loops.
Code example
The following code accesses both variables a
and b
with a stride LEN
.
for (int i = 0; i < LEN; ++i) {
for (int j = 1; j < LEN; j++) {
a[j * LEN + i] = a[j * LEN + i - 1] + b[j * LEN + i];
}
}
By doing loop interchange of loops over a
and b
, we get rid of the strided accesses with regard to the innermost loop.
for (int j = 1; j < LEN; ++j) {
for (int i = 0; i < LEN; i++) {
a[j * LEN + i] = a[j * LEN + i - 1] + b[j * LEN + i];
}
}

Building performance into the code from day one with Codee