C ++ 2011:基于范围的循环展开? [英] C++ 2011 : range-based loop unrolling?

查看:204
本文介绍了C ++ 2011:基于范围的循环展开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道C ++编译器是否会展开基于范围的循环,就像他们目前为正常循环所做的那样,以最大限度地提高性能,或者在某些情况下基于范围的循环会比正常循环慢?

$ b $基于范围的循环等效于

解决方案

>

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

迭代次数,它可以解决循环依赖性或循环是独立的,然后编译器可以自由滚动。



一般来说,循环展开只会改善较小循环的性能。所以,IMO,不管基于范围的循环是否展开无关紧要。你当然可以使用 -O3 -funroll-loops 和相关选项进行基准化,看看两者之间是否有任何区别。


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 ?

Thank you very much.

解决方案

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.

这篇关于C ++ 2011:基于范围的循环展开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆