Why is a CPU branch instruction slow?

前端 未结 4 1017
执笔经年
执笔经年 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:14

    A branch instruction is not inherently slower than any other instruction.

    However, the reason you heard that branches should avoided is because modern CPUs follow a pipeline architecture. This means that there are multiple sequential instructions being executed simultaneously. But the pipeline can only be fully utilised if it's able to read the next instruction from memory on every cycle, which in turn means it needs to know which instruction to read.

    On a conditional branch, it usually doesn't know ahead of time which path will be taken. So when this happens, the CPU has to stall until the decision has been resolved, and throws away everything in the pipeline that's behind the branch instruction. This lowers utilisation, and therefore performance.

    This is the reason that things like branch prediction and branch delay slots exist.

提交回复
热议问题