Performance of branch prediction in a loop

心不动则不痛 提交于 2019-12-04 12:38:04

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:

thereby making both code-snippets emit exactly same machine instructions.

You asked a performance question without specifying hardware (although from the question we can infer that it's one of the architectures that have branch prediction), toolchain, or compile options.

Overall, this is just another space vs speed tradeoff, where space often itself affects speed (CPU instruction and microcode caches).

The only reasonable answer is "Performance will vary depending on processor hardware and compiler optimizations."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!