Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ 2011 : range-based loop unrolling?

I wonder if C++ compilers will unroll range-based loop the same way they currently do for "normal" loops to maximize performance or in some case range-based loops will be slower than normal loops?

like image 848
Vincent Avatar asked Mar 17 '26 07:03

Vincent


1 Answers

for range-based loop is equivalent to:

{
  auto && __range = ( /expression/ );
  for (auto __begin = begin(__range),
            __end   = end(__range);
       __begin != __end;
       ++__begin) {
    /declaration/ = *__begin;
    /statement/
  }
}

If compiler knows the number of iterations and it can solve the loop dependencies or loops are independent, then compiler is free to un-roll.

In general, loop unrolling is going to improve performance only for smaller loops. So, IMO, it does not matter whether range-based loops are unrolled or not. You can certainly benchmark with -O3 and -funroll-loops and relevant options to see if there's indeed any difference between two.

like image 193
P.P Avatar answered Mar 19 '26 21:03

P.P