'if'是否有性能开销 [英] Does 'if' have performance overhead

查看:114
本文介绍了'if'是否有性能开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在教程的某处我读到if语句有性能

开销,因为if语句中的代码不能从微处理器的管道连接中获益,而且编译器也不能

积极优化该代码。

Somewhere in a tutorial i read that if statement has performance
overheads as code within the if statement cannot take benefit of
pipeling of microprocessor and also that the compiler cannot
agressively optimize that code.


>从那天开始,我一直试图避免if语句带有
>From that day onwards i have been trying to avoid if statement withing



我的功能尽可能多,并尝试使用最少的代码

withing if。


然而,我我对此持怀疑态度。

我需要一些指导。对于我来说,性能始终是关键问题。

它来写程序。


请指导。

my functions as much as possible and also try to have minimum code
withing if block.

However, i am bit skeptic about this.
I need some guidance. Performance is always the key issue for me when
it comes to writing programs.

Please guide.

推荐答案

la******@gmail.com 写道:
la******@gmail.com wrote:

在教程的某处我读到if语句有性能

开销因为if语句中的代码无法获益

管道微处理器以及编译器无法实现
积极优化该代码。
Somewhere in a tutorial i read that if statement has performance
overheads as code within the if statement cannot take benefit of
pipeling of microprocessor and also that the compiler cannot
agressively optimize that code.



我听说有点真实。


这不是if语句本身。事实上,

计划分支的控制流程。

Somewhat true, I hear.

It''s not the if-statement per se. It''s the fact that control flow of the
program branches.


从那天起我一直在尝试为了避免if语句尽可能多地使用我的函数,并尝试使用最小代码

withing if block。
From that day onwards i have been trying to avoid if statement withing
my functions as much as possible and also try to have minimum code
withing if block.



这是一个坏主意(tm)。


优化避免if语句通常不明显并改变

基本上是你的代码的本质。要看到这一点,只需尝试重写


unsigned int max(unsigned int lhs,unsigned int rhs);


没有分支语句。通常,避免分支语句可以被视为代码混淆。为了性能而进行代码混淆

通过分析未证明必要的收益是一种过早优化的形式。

That is a Bad Idea(tm).

Optimizations avoiding if-statements are usually non-obvious and change the
nature of your code fundamentally. To see that, just try to rewrite

unsigned int max ( unsigned int lhs, unsigned int rhs );

without branch statements. Generally, avoiding branch statements can be
considered code obfuscation. Code obfuscation for the sake of performance
gains that have not proven necessary by profiling is a form of premature
optimization.


但是,我对此持怀疑态度。
However, i am bit skeptic about this.



你的测量结果告诉你什么?

What do your measurements tell you?


我需要一些指导。在编写程序时,性能始终是我的关键问题。
I need some guidance. Performance is always the key issue for me when
it comes to writing programs.



不应该。程序员时间比CPU时间贵得多。

理性选择是优化代码美观,可扩展性,易用性,

和可维护性。

最佳


Kai-Uwe Bux

It should not be. Programmer time is much more expensive than CPU time. The
rational choice is to optimize for code beauty, extendability, ease of use,
and maintenability.
Best

Kai-Uwe Bux


< la ****** @ gmail.comwrote in message

news: 11**********************@q3g2000prf.googlegro ups.com ...
<la******@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...

某处在一个教程中,我读到if语句有性能

开销,因为if语句中的代码不能利用微处理器的b / b
管道编译器而且编译器也无法使用

积极优化该代码。
Somewhere in a tutorial i read that if statement has performance
overheads as code within the if statement cannot take benefit of
pipeling of microprocessor and also that the compiler cannot
agressively optimize that code.

>>从那天开始,我一直试图避免if语句带有
>>From that day onwards i have been trying to avoid if statement withing



我的功能尽可能多,并尝试使用最少的代码

如果阻止。


但是,我有点对此持怀疑态度。


我需要一些指导。对于我来说,性能始终是关键问题。

它来写程序。


请指导。

my functions as much as possible and also try to have minimum code
withing if block.

However, i am bit skeptic about this.

I need some guidance. Performance is always the key issue for me when
it comes to writing programs.

Please guide.



你不应该过早地优化。也就是说,不要尝试优化

代码,直到找到实际花费时间的代码。


如果语句在代码中极为常见语言他们基本上是基于某些条件(如果不是零则跳跃,如果大于零则跳跃为
,等等......)。现在,我相信你正在谈论

预取指令,而且他不知道在没有跳转的情况下,如果有跳转,设置A或者CPU不知道要预取的指令集是什么?设置B.


很多时候编译器本身可能会对代码进行优化,并且很多时候这些CPU很聪明,要么想出来或预取两个分支。

换句话说,我不担心判断是否需要花费太多时间。虽然如果没有任何理由我也不会投入。

You should not prematurely optimize. That is, don''t attempt to optimize
code until you find what is actually taking the time.

if statements are extremly common in code in all languages They essentially
come down to a jump in the CPU based on some condition (jump if not zero,
jump if greater than zero, etc...). Now, I believe you are talking about
prefetching instructions and that he CPU won''t know what set of instructions
to prefetch if there is a jump, set A or set B.

A lot of times the compiler itself may optimize the code, and a lot of times
the CPUs are smart enough to either figure it out or prefetch both branches.
In other words, I would not worry about if statments taking up too much
time. Although I wouldn''t throw in if statments for no reason either.


在2007-10-16 07:25, la ****** @ gmail.com 写道:
On 2007-10-16 07:25, la******@gmail.com wrote:

在教程的某处我读到了如果语句有性能

开销,因为if语句中的代码不能从微处理器的管道连接中受益,而且编译器也无法优化
积极优化那段代码。
Somewhere in a tutorial i read that if statement has performance
overheads as code within the if statement cannot take benefit of
pipeling of microprocessor and also that the compiler cannot
agressively optimize that code.



这完全不在话题:


这只是现代PC处理器的一部分(相对于

嵌入式处理器,我对它知之甚少),因为它们所有

现在都有相当不错的分支预测。最好通过一个简单的循环演示




for(int i = 0; i< 10; ++ i)

{

//做东西

}

//做其他的东西


当处理器看到一个循环时,它会识别一个循环,并且它将假定您将执行迭代,因此循环可以非常好地优化。当最后一次迭代完成时,问题就出现了,因为

处理器错误地认为你会迭代你发现你没有得到一个小的b
性能命中。同样,处理器可以优化if语句和其他控制

语句。更好的是,他们可以学习,所以如果你有一个if语句

并且你一次又一次地去了else子句,处理器将会记住这个并且将开始执行在比较完成之前到达

if语句时的else子句。再说一遍,如果它发生了,那么假设错误就会导致性能下降。


请注意,虽然这种性能上的提升在现代的

硬件比P4上的硬件要多,因为今天的处理器并不像B / B
那样深度流水线化。

This is totally off topic:

That is only partially true one a modern PC processor (as opposed to
embedded processors which I have little knowledge about) since they all
have pretty good branch prediction these days. This is best demonstrated
by a simple loop:

for (int i = 0; i < 10; ++i)
{
// do stuff
}
// do other stuff

The processor recognises a loop when it sees one, and it will assume
that you will perform the iterations, so the loop can be optimised very
well. The problem comes when the last iteration is done, since the
processor wrongly assumes that you will iterate you get a small
performance hit when it discovers that you do not.

Similarly the processor can optimise if statements and other control
statements. Even better, they can learn, so if you have an if statement
and you time after time go to the else clause the processor will
remember this and will start executing the else clause when reaching the
if statement before the comparison is complete. Again, should it happen
that the assumption is wrong you get a performance hit.

Notice though that this performance hit is less noticeable on modern
hardware than is was on a P4, since the processors of today are not as
deeply pipelined.


>>从那天开始,我一直试图避免if语句带有
>>From that day onwards i have been trying to avoid if statement withing



我的功能尽可能多,并尝试拥有最小代码

with if if block。

my functions as much as possible and also try to have minimum code
withing if block.



优化if语句的最佳方法是编写最有可能在if子句中执行的代码
最不可能在

else子句中,因为在大多数情况下,这将使处理器保持跳跃状态。

The best way to optimise an if statement is to write the code that is
most likely to be executed in the if clause and the least likely in the
else clause, since that will save the processor a jump in most cases.


但是,我有点怀疑这个。
However, i am bit skeptic about this.



你应该是这样,你应该怀疑任何优化,即
不在算法级别。

Rightly you should be, you should be sceptic about any optimisation that
is not at the algorithmic level.


我需要一些指导。在编写程序时,性能始终是我的关键问题。
I need some guidance. Performance is always the key issue for me when
it comes to writing programs.



为任务选择最佳算法和数据结构,然后使用

a good profiler。


-

Erik Wikstr ?? m

Select the best algorithms and data structures for the task and then use
a good profiler.

--
Erik Wikstr??m


这篇关于'if'是否有性能开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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