Memory access comparison
<p>Which one of the 2 is faster (C++)?</p>

<pre><code>for(i=0; i&lt;n; i++)
{
    sum_a = sum_a + a[i];
    sum_b = sum_b + b[i];
}
</code></pre>

<p>Or</p>

<pre><code>for(i=0; i&lt;n; i++)
{
    sum_a = sum_a + a[i];
}
for(i=0; i&lt;n; i++)
{
    sum_b = sum_b + b[i];
}
</code></pre>

<p>I am a beginner so I don't know whether this makes sense, but in the first version, array 'a' is accessed, then 'b', which might lead to many memory switches, since arrays 'a' and 'b' are at different memory locations. But in the second version, whole of array 'a' is accessed first, and then whole of array 'b', which means continuous memory locations are accessed instead of alternating between the two arrays. </p>

<p>Does this make any difference between the execution time of the two versions (even a very negligible one)?</p>