Which of the two is faster (C ++)?
for(i=0; i<n; i++) { sum_a = sum_a + a[i]; sum_b = sum_b + b[i]; }
or
for(i=0; i<n; i++) { sum_a = sum_a + a[i]; } for(i=0; i<n; i++) { sum_b = sum_b + b[i]; }
I am new, so I donβt know if this makes sense, but in the first version the array βaβ and then βbβ are available, which can lead to many memory switches, since the arrays βaβ and βbβ are in different cells memory. But in the second version, the entire array "a" is accessed first, and then the entire array "b", which means access to permanent memory cells instead of alternating between two arrays.
Does it really matter between the runtime of two versions (even very small)?
source share