分支预测在循环中的性能 [英] Performance of branch prediction in a loop
问题描述
这两个代码段之间会有明显的速度差异吗?原来,我认为第二个片段会更快,因为分支指令会遇到很少,但另一方面,分支预测器应该解决这个问题。或者它会有明显的开销,尽管可预测的模式?假设不使用条件移动指令。
Would there be any noticeable speed difference between these two snippets of code? Naively, I think the second snippet would be faster because branch instructions are encountered a lot less, but on the other hand the branch predictor should solve this problem. Or will it have a noticeable overhead despite the predictable pattern? Assume that no conditional move instruction is used.
代码段1:
for (int i = 0; i < 100; i++) {
if (a == 3)
output[i] = 1;
else
output[i] = 0;
}
$ b
Snippet 2:
if (a == 3) {
for (int i = 0; i < 100; i++)
output[i] = 1;
} else {
for (int i = 0; i < 100; i++)
output[i] = 0;
}
我不打算自己优化这些情况,
I'm not intending to optimise these cases myself, but I would like to know more about the overhead of branches even with a predictable pattern.
推荐答案
由于 a
保持不变一旦你进入循环,这两个代码片段之间应该没有太大的区别。
Since a
remains unchanged once you enter into the loop, there shouldn't be much difference between the two code-snippet.
就个人而言,我喜欢使用前者,除非分支预测器无法预测真的不可能的分支,因为
Personally, I would prefer the former, unless branch predictor fails to predict the branch which is really unlikely, given that a
remains unchanged in the loop.
此外,编译器可以执行此优化:
Moreover, the compiler may perform this optimization:
- Loop unswitching
,从而使两个代码段产生完全相同的机器指令。
thereby making both code-snippets emit exactly same machine instructions.
这篇关于分支预测在循环中的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!