如果语句相比,测试,如果不使用一个效率如何为? (C ++) [英] How efficient is an if statement compared to a test that doesn't use an if? (C++)

查看:91
本文介绍了如果语句相比,测试,如果不使用一个效率如何为? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个计划,让两个数字越小,如果使用的是标准的如果x小于y我不知道

  INT A,B,低;
如果(A< B)低=一;
其他低= B;

比这更多或更少效率更高:

  INT A,B,低;
低= B +((一 - 二)及((一 - 二)及GT;> 31));

(或将 INT三角洲= a的变化 - B 在顶部和rerplacing 的实例的 - B 与)。

我只是想知道其中哪一个会更有效(或者如果差异过微乎其微的是相关的),和if-else语句与一般替代品的效率。


解决方案

(免责声明:非常低级别的优化,最常见的是没有必要以下的交易。如果你继续读书,你放弃你的权利抱怨说电脑是快速,从未有任何理由担心这样的事情。)

消除了如果语句的一个优点是避免分支prediction处罚。

分公司prediction处罚的一般都只有一个问题,当分支不容易predicted。一个分支是很容易$时,它几乎总是采取/不采取p $ pdicted,或者它遵循一个简单的模式。例如,在一个循环语句中的分支,除了最后一个每一次,所以很容易predicted。但是,如果你有code像

  A =随机()%10
如果(一个小于5)
  打印少
其他
  打印大

那么这个分支是不容易predicted,并且经常会产生与清除缓存和回退是在分支的错误部分执行的指令相关的prediction点球。

,以避免这类惩罚的一种方式是使用三元(:<?/ code>)运算符。在简单情况下,编译器将生成条件移动指令,而不是分公司。<​​/ P>

所以

  INT A,B,低;
如果(A&LT; B)低=一;
其他低= B;

变为

  INT A,B,低;
低=(A&LT; B)? A:B

和在第二种情况下的分支指令是没有必要的。此外,它比你的位变换实现更清晰,更具可读性。

当然,这是一个微型的优化是不太可能对您的code显著的影响。

I need a program to get the smaller of two numbers, and I'm wondering if using a standard "if x is less than y"

int a, b, low;
if (a < b) low = a;
else low = b;

is more or less efficient than this:

int a, b, low;
low = b + ((a - b) & ((a - b) >> 31));

(or the variation of putting int delta = a - b at the top and rerplacing instances of a - b with that).

I'm just wondering which one of these would be more efficient (or if the difference is too miniscule to be relevant), and the efficiency of if-else statements versus alternatives in general.

解决方案

(Disclaimer: the following deals with very low-level optimizations that are most often not necessary. If you keep reading, you waive your right to complain that computers are fast and there is never any reason to worry about this sort of thing.)

One advantage of eliminating an if statement is that you avoid branch prediction penalties.

Branch prediction penalties are generally only a problem when the branch is not easily predicted. A branch is easily predicted when it is almost always taken/not taken, or it follows a simple pattern. For example, the branch in a loop statement is taken every time except the last one, so it is easily predicted. However, if you have code like

a = random() % 10
if (a < 5)
  print "Less"
else
  print "Greater"

then this branch is not easily predicted, and will frequently incur the prediction penalty associated with clearing the cache and rolling back instructions that were executed in the wrong part of the branch.

One way to avoid these kinds of penalties is to use the ternary (?:) operator. In simple cases, the compiler will generate conditional move instructions rather than branches.

So

int a, b, low;
if (a < b) low = a;
else low = b;

becomes

int a, b, low;
low = (a < b) ? a : b

and in the second case a branching instruction is not necessary. Additionally, it is much clearer and more readable than your bit-twiddling implementation.

Of course, this is a micro-optimization which is unlikely to have significant impact on your code.

这篇关于如果语句相比,测试,如果不使用一个效率如何为? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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