Is there any difference in performance in one big loop vs many small loops? I need to iterate over 3 arrays with 64 elements each, and I also have to check for each item if it is NULL, if it is, I skip that iteration. Would it be better to write one big loop and insert 3 ifs inside of it, or write 3 small loops, one for each array?
A big loop could be faster because it has fewer branches in total (4 vs 6) and is slightly less code overall.
3 small branches will probably be faster on most modern CPU's because the cost of the branches for the loops is small, and splitting the loops up can make instruction fetching more efficient.
This is highly dependent on how much code you have in the loops, and also potentially on the positions of your branches within those loops. Branch prediction could be affected too, but this could go either way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With