c - Dereferencing iterators performance -
i have 3 functions come down code below, runs 800x800 times:
each while loop below runs 800 times before iter1 == lim, duration measured ran 800x800x800 (512 millions) times.
iter1, iter2 , lim double pointers. point large enough vector of double's.
sum double local variable.
s1 , s2 local unsigned int's, both equal 800.
first runs in 2.257 seconds:
while ( iter1 < lim ) { sum += *iter1 * *iter2; ++iter1; iter2 += s2; } second runs in 7.364 seconds:
while ( iter1 < lim ) { sum += *iter1 * *iter2; iter1 += s1; iter2 += s2; } third runs in 1.355 seconds:
while ( iter1 < lim ) { sum += *iter1 * *iter2; ++iter1; ++iter2; } if remove sum += *iter1 * *iter2; instruction each of them, run in around 1.07 seconds.
if remove second multiplication , change instruction sum += *iter1;, first , third run in 1.33 seconds, while second runs in 1.46 seconds.
if remove other iterator, this: sum += *iter2;, first , second run in around 2.2 seconds, while third runs in 1.35 seconds.
obviously, performance drop tied quantity added iter1 , iter2. no expert in how processor accesses memory , dereferences pointers, hope in community knows more me , willing shed light on problem.
if need information hardware ran these tests on, or else can prove helpful, feel free ask in comments.
edit: problem second function slow, when compared others, , wanted know if there can make run faster, appeared doing similar things other 2.
edit 2: measurements made in release build
this manifestation of data locality. takes less time @ @ next page of book @ 800th next page. try @ home.
Comments
Post a Comment