为什么一个CPU转移指令慢? [英] Why is a CPU branch instruction slow?

查看:142
本文介绍了为什么一个CPU转移指令慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我开始编程,我看过的每一个地方,以避免浪费分支不惜一切代价。

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

这很好,虽然没有任何文章解释了为什么我应该这样做。到底发生了什么时, CPU 德codeS一个分支指令,并决定做一个跳跃?什么是物,使得它比其它指令(如加法)?

That's fine, although none of the articles explained why I should do this. What exactly happens when the CPU decodes a branch instruction and decides to do a jump? And what is the "thing" that makes it slower than other instructions (like addition)?

推荐答案

一个分支指令比其他任何指令并非天生慢。

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

不过,你听说过没有分支机构要避免的原因是因为现代的CPU遵循管线架构。这意味着,有正在同时执行多个顺序的指令。但管道只能充分,如果是能够从存储器读的下一个指令在每个周期,这又意味着它需要知道的其中指令读取利用。

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.

在一个的条件的分支,它通常不会提前知道哪条路径将采取时间。因此,当这种情况发生时,CPU有来搪塞,直到确定已经解决,并丢弃在管道这就是分支指令后面的一切。这降低了利用率,并因此表现

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.

这是什么原因,事情如分支prediction 分支延迟槽存在。

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

这篇关于为什么一个CPU转移指令慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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