c++ - Memory access comparison -
which 1 of 2 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 beginner don't know whether makes sense, in first version, array 'a' accessed, 'b', might lead many memory switches, since arrays 'a' , 'b' @ different memory locations. in second version, whole of array 'a' accessed first, , whole of array 'b', means continuous memory locations accessed instead of alternating between 2 arrays.
does make difference between execution time of 2 versions (even negligible one)?
i don't think there correct answer question. in general, second version has more twice iterations (cpu execution overhead), worse access memory (memory access overhead). imagine run code on pc has slow clock, insanely cache. memory overhead gets reduced, since clock slow running same loop twice makes execution longer. other way around: fast clock, bad memory - running 2 loops not problem, it's better optimize memory access.
here cool example on how can profile app: link
Comments
Post a Comment