Why is a CPU branch instruction slow?

前端 未结 4 1011
执笔经年
执笔经年 2020-12-05 00:29

Since I started programming, I have read in every place to avoid wasteful branches at all costs.

That\'s fine, although none of the articles explained why I should d

4条回答
  •  半阙折子戏
    2020-12-05 01:20

    This is not completely related, but advice to avoid branches at all cost is simply nonsense on modern speculative, out-of-order execution processors. Speculative execution is exactly the thing that gives your processor instructions to process while waiting for data from the memory. And speculation on branch conditions is what speculative execution is all about. Replacing branches with arithmetic can actually slow down your program so beware! More about here.

提交回复
热议问题