c++ - Is it faster to iterate through the elements of an array with pointers incremented by 1? -


this question has answer here:

is faster

for ( int * pa(arr), * pb(arr+n); pa != pb; ++pa ) {     // *pa } 

than

for ( size_t k = 0; k < n; ++k ) {     // arr[k] } 

???

i understand arr[k] equivalent *(arr+k), in first method using current pointer has incremented 1, while in second case using pointer incremented arr successively larger numbers. maybe hardware has special ways of incrementing 1 , first method faster? or not? curious. hope question makes sense.

if compiler smart enought (and of compilers is) performance of both loops should ~equal.

for example have compiled code in gcc 5.1.0 generating assembly:

int __attribute__ ((noinline)) compute1(int* arr, int n) {   int sum = 0;   for(int = 0; < n; ++i)   {     sum += arr[i];   }   return sum; }  int __attribute__ ((noinline)) compute2(int* arr, int n) {   int sum = 0;   for(int * pa(arr), * pb(arr+n); pa != pb; ++pa)   {     sum += *pa;   }   return sum; } 

and result assembly is:

compute1(int*, int):     testl   %esi, %esi     jle .l4     leal    -1(%rsi), %eax     leaq    4(%rdi,%rax,4), %rdx     xorl    %eax, %eax .l3:     addl    (%rdi), %eax     addq    $4, %rdi     cmpq    %rdx, %rdi     jne .l3     rep ret .l4:     xorl    %eax, %eax     ret compute2(int*, int):     movslq  %esi, %rsi     xorl    %eax, %eax     leaq    (%rdi,%rsi,4), %rdx     cmpq    %rdx, %rdi     je  .l10 .l9:     addl    (%rdi), %eax     addq    $4, %rdi     cmpq    %rdi, %rdx     jne .l9     rep ret .l10:     rep ret main:     xorl    %eax, %eax     ret 

as can see, heavy part (loop) of both functions equal:

.l9:     addl    (%rdi), %eax     addq    $4, %rdi     cmpq    %rdi, %rdx     jne .l9     rep ret 

but in more complex examples or in other compiler results might different. should test , measure, of compilers generate similar code.

the full code sample: https://goo.gl/mpqss0


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -