for-loop order
Show older comments
I am wondering whether the order of for-loop affects the speed. For example, I am running multiple for-loops and they have different number of elements.
for i=1:n
for j=1:m
for k=1:p
....
end
end
end
In this case, is it better to place the loop with the smallest number into the outer loop ? or doesn't it matter?
1 Comment
dpb
on 17 Jul 2018
The loops themselves don't matter; what does matter (and can be huge impact for large arrays) is to address data in column major order to avoid cache misses by addressing elements in sequential order. This implies advancing leftmost subscripts first.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!