在C ++中使用gcc进行恒定嵌入式循环条件优化 [英] Constant embedded for loop condition optimization in C++ with gcc

查看:113
本文介绍了在C ++中使用gcc进行恒定嵌入式循环条件优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器是否会优化tihs:

  bool someCondition = someVeryTimeConsumingTask(/ * ... * /); 

for(int i = 0; i< HUGE_INNER_LOOP; ++ i)
{
if(someCondition)
doCondition(i);
else
培根(i);

转换为:

  bool someCondition = someVeryTimeConsumingTask(/ * ... * /); 

if(someCondition)
for(int i = 0; i< HUGE_INNER_LOOP; ++ i)
doCondition(i);
else
for(int i = 0; i< HUGE_INNER_LOOP; ++ i)
bacon(i);



someCondition在for循环中是常数不变的。



这看起来很明显,我应该自己做,但是如果你有多个条件,那么你正在处理for循环的permuatations,所以代码会长得多。我正在决定是否这样做(我已经在优化)或是否浪费我的时间。

编译器可能会像你一样编写代码,但我从来没有见过这样的优化。



然而在现代CPU中有一种叫做分支预测的东西。实质上,这意味着当处理器被要求执行条件跳转时,它会在评估条件之前开始执行被认为是最可能的分支。这是为了保持流水线充满指令。

如果处理器失败(并且坏分支),它会导致管道刷新:它被称为错误预测。

这个特性的一个非常普遍的特点是,如果同一个测试连续多次产生相同的结果,那么它会被认为产生相同的结果通过分支预测算法......这当然是为循环量身定制的:)

它让我微笑,因为你担心如果中为主体,而本身导致分支预测>>条件必须为每次迭代评估以检查是否继续;)

因此,不用担心它,它的成本低于缓存未命中。



现在,如果你真的担心这个问题,总会有仿函数的方法。

  typedef void(* functor_t)(int); 

functor_t func = 0;
if(someCondition)func =& doCondition;
else func =& bacon;

for(int i = 0; i

这看起来好多了,不是吗?明显的缺点是兼容签名的必要性,但是你可以为函数编写包装器。只要你不需要打破/返回,你就会好起来的。否则,如果在循环体中需要一个:D


Will a compiler optimize tihs:

bool someCondition = someVeryTimeConsumingTask(/* ... */);

for (int i=0; i<HUGE_INNER_LOOP; ++i)
{
    if (someCondition)
        doCondition(i);
    else
        bacon(i);
}

into:

bool someCondition = someVeryTimeConsumingTask(/* ... */);

if (someCondition)
    for (int i=0; i<HUGE_INNER_LOOP; ++i)
        doCondition(i);
else
    for (int i=0; i<HUGE_INNER_LOOP; ++i)
        bacon(i);

someCondition is trivially constant within the for loop.

This may seem obvious and that I should do this myself, but if you have more than one condition then you are dealing with permuatations of for loops, so the code would get quite a bit longer. I am deciding on whether to do it (I am already optimizing) or whether it will be a waste of my time.

解决方案

It's possible that the compiler might write the code as you did, but I've never seen such optimization.

However there is something called branch prediction in modern CPU. In essence it means that when the processor is asked to execute a conditional jump, it'll start to execute what is judged to be the likeliest branch before evaluating the condition. This is done to keep the pipeline full of instructions.

In case the processor fails (and takes the bad branch) it cause a flush of the pipeline: it's called a misprediction.

A very common trait of this feature is that if the same test produce the same result several times in a row, then it'll be considered to produce the same result by the branch prediction algorithm... which is of course tailored for loops :)

It makes me smile because you are worrying about the if within the for body while the for itself causes a branch prediction >> the condition must be evaluated at each iteration to check whether or not to continue ;)

So, don't worry about it, it costs less than a cache miss.

Now, if you really are worried about this, there is always the functor approach.

typedef void (*functor_t)(int);

functor_t func = 0;
if (someCondition) func = &doCondition;
else func = &bacon;

for (int i=0; i<HUGE_INNER_LOOP; ++i) (*func)(i);

which sure looks much better, doesn't it ? The obvious drawback is the necessity for compatible signatures, but you can write wrappers around the functions for that. As long as you don't need to break/return, you'll be fine with this. Otherwise you would need a if in the loop body :D

这篇关于在C ++中使用gcc进行恒定嵌入式循环条件优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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