代码优化 - 删除for循环中的if .. else条件 [英] code optimization- Removing if .. else conditions in for loops

查看:91
本文介绍了代码优化 - 删除for循环中的if .. else条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我需要帮助去除for循环中的..else条件。我用

使用了以下方法,但我不确定它是否实际上有帮助。

下面是一个例子来说明我有什么使用。

原始代码:


c = 0;

for(i = 0; i< 999; i ++)

{

if(a> B)

c ++;

}


速度优化的修改代码


c = 0;

for(i = 0; i< 999; i ++)

{

c + =(a> B);

}


根据我的推理,评估逻辑表达式但是没有

应该生成条件分支指令。这应该

避免任何管道摊位。

但是,我没有找到任何确认我信仰的文件!

任何评论,建议或参考将是一个很大的帮助!


谢谢&问候

Hello,

I need help in removing if ..else conditions inside for loops. I have
used the following method but I am not sure whether it has actually
helped.
Below is an example to illustrate what I have used.
Original code :

c= 0 ;
for (i=0; i<999; i++)
{
if (a > B)
c++ ;
}

Modified code for speed optimization

c= 0 ;
for (i=0; i<999; i++)
{
c += (a > B) ;
}

As per my reasoning, the logical expression is evaluated but no
conditional branching instructions should be generated. This should
avoid any pipeline stalls.
However, I havent found any document confirming my belief !
Any comments, suggestions or references will be a big help !

Thanks & Regards

推荐答案

" Kunal" <ク************* @ gmail.com>写道:
"Kunal" <ku*************@gmail.com> writes:
我需要帮助去除for循环中的..else条件。我已经使用了以下方法,但我不确定它是否真的有帮助。
下面是一个例子来说明我使用过的。
原始代码:

c = 0;
for(i = 0; i< 999; i ++)
{
if(a> B)
c ++;
}

用于速度优化的修改代码

c = 0;
for(i = 0; i< 999; i ++)
{
c + =(a> B);
}
I need help in removing if ..else conditions inside for loops. I have
used the following method but I am not sure whether it has actually
helped.
Below is an example to illustrate what I have used.
Original code :

c= 0 ;
for (i=0; i<999; i++)
{
if (a > B)
c++ ;
}

Modified code for speed optimization

c= 0 ;
for (i=0; i<999; i++)
{
c += (a > B) ;
}




唯一可以确定这是否有所作为的方法是
通过测量
。我的猜测是,它与大多数编译器没什么区别。


注意评估'a> b''本身可能需要一个分支。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while( * q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);} return 0;}



The only way you can find out whether this makes a difference is
by measuring. My guess is that it will make no difference with
most compilers.

Note that the evaluation of `a > b'' may itself require a branch.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


" Kunal" <ク************* @ gmail.com>写道:
"Kunal" <ku*************@gmail.com> writes:
我需要帮助去除for循环中的..else条件。我已经使用了以下方法,但我不确定它是否真的有帮助。
下面是一个例子来说明我使用过的。
原始代码:

c = 0;
for(i = 0; i< 999; i ++)
{
if(a> B)
c ++;
}

用于速度优化的修改代码

c = 0;
for(i = 0; i< 999; i ++)
{
c + =(a> B);
}
根据我的推理,评估逻辑表达式但不应生成条件分支指令。这应该可以避免任何管道停滞。
但是,我没有找到任何证实我信仰的文件!
任何评论,建议或参考都将是一个很大的帮助!
I need help in removing if ..else conditions inside for loops. I have
used the following method but I am not sure whether it has actually
helped.
Below is an example to illustrate what I have used.
Original code :

c= 0 ;
for (i=0; i<999; i++)
{
if (a > B)
c++ ;
}

Modified code for speed optimization

c= 0 ;
for (i=0; i<999; i++)
{
c += (a > B) ;
}

As per my reasoning, the logical expression is evaluated but no
conditional branching instructions should be generated. This should
avoid any pipeline stalls.
However, I havent found any document confirming my belief !
Any comments, suggestions or references will be a big help !




你假设为(a> B)生成的代码不会涉及任何

条件分支。


任何体面的编译器都可能为这两种形式生成相似或相同的代码




通常最好将代码写得清楚尽可能让编译器担心微优化问题。试图自己进行低级优化可能会让编译器的工作更加困难。


优化规则:不要这样做。

优化的第二条规则(仅限高级用户):暂不做。


如果你真的需要做这种微优化,也许是b $ b因为循环是你程序性能关键的部分

(因为你测量过它) ),您可以检查编译器生成的汇编语言 - 或者您可以自己编写

汇编语言的代码。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



You''re assuming that the code generated for (a > B) won''t involve any
conditional branches.

Any decent compiler is likely to generate similar or identical code
for both forms.

It''s usually best to write your code as clearly as possible and let
the compiler worry about micro-optimization. Attempting to do
low-level optimization yourself is likely to make the compiler''s job
more difficult.

First rule of optimization: Don''t do it.
Second rule of optimization (advanced users only): Don''t do it yet.

If you really need to do this kind of micro-optimization, perhaps
because the loop is in a performance-critical part of your program
(because you''ve measured it), you can probably examine the assembly
language generated by your compiler -- or you can write the code in
assembly language yourself.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


在文章< 11 ********************** @ g14g2000cwa.googlegroups .com>,

" Kunal" <ク************* @ gmail.com>写道:
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Kunal" <ku*************@gmail.com> wrote:
你好,

我需要帮助删除if循环中的..else条件。我已经使用了以下方法,但我不确定它是否真的有帮助。
下面是一个例子来说明我使用过的。
原始代码:

c = 0;
for(i = 0; i< 999; i ++)
{
if(a> B)
c ++;
}

用于速度优化的修改代码

c = 0;
for(i = 0; i< 999; i ++)
{
c + =(a> B);
}
根据我的推理,评估逻辑表达式但不应生成条件分支指令。这应该可以避免任何管道停滞。
Hello,

I need help in removing if ..else conditions inside for loops. I have
used the following method but I am not sure whether it has actually
helped.
Below is an example to illustrate what I have used.
Original code :

c= 0 ;
for (i=0; i<999; i++)
{
if (a > B)
c++ ;
}

Modified code for speed optimization

c= 0 ;
for (i=0; i<999; i++)
{
c += (a > B) ;
}

As per my reasoning, the logical expression is evaluated but no
conditional branching instructions should be generated. This should
avoid any pipeline stalls.



您是否测量了需要多长时间?


最重要的优化规则:不要优化,直到你有
测量它。


(有几个最重要的优化规则,这只是

其中一个。我打赌会发布更多内容。这个一直适用:

永远不要在没有测量的情况下进行优化。)


Did you measure how long it takes?

The most important rule of optimisation: Don''t optimise until you have
measured it.

(There are several most important rules of optimisation, this is just
one of them. I bet some more will be posted. This one always applies:
Never, ever optimise without measuring. )


这篇关于代码优化 - 删除for循环中的if .. else条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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