简体   繁体   English

是否有可能帮助分支预测?

[英]Is it possible to aid branch prediction?

Can you intentionally write code in a specific way so that the branch predictor will choose the option that will be the case most of the times. 您是否可以有意识地以特定方式编写代码,以便分支预测器将选择大多数情况下的选项。 For example error checks whether a resource was loaded. 例如,错误检查资源是否已加载。 If this is possible how can you use this to your advantage? 如果可能的话,你如何利用这个优势?

If you are using GCC you can use the macros `likely()/unlikely()': 如果你正在使用GCC,你可以使用宏`possible()/ impossible()':

likely()/unlikely() macros in the Linux kernel - how do they work? Linux内核中的可能()/不可能()宏 - 它们如何工作? What's their benefit? 他们的好处是什么?

Theoretically, yes. 从理论上讲,是的。 Effectively speaking NO. 有效地说NO。 You won't really get any benefit, try it out yourself. 你不会真正得到任何好处,自己尝试一下。

With the way modern hardware works your CPU will still grind out all of the branches no matter what you do. 随着现代硬件的运行方式,无论您做什么,您的CPU仍然会磨掉所有分支。 But it doesn't really matter because they will do it concurrently. 但这并不重要,因为他们会同时做到这一点。

To attempt to do it yourself you would need to use assembly language. 要尝试自己动手,您需要使用汇编语言。 Compiler hints like shown above will not do much. 上面显示的编译器提示不会做太多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分支预测不起作用吗? - Is branch prediction not working? Java for循环中的分支预测 - Branch prediction in a java for loop 为什么分支预测比没有分支更快? - Why is branch prediction faster than no branch at all? 如何在 Java 中分析分支预测命中率 - How to profile branch prediction hitrate in Java JVM 有它的运行时分支预测吗? 如果是这样,Java 代码中是否有提示分支结果的方法? - Does JVM has its runtime branch prediction? If so, is there a way in Java code to hint branch results? 分支预测:避免简单操作的“else”分支是否会使代码更快(Java 示例)? - Branch prediction: Does avoiding “else” branch for simple operations makes code faster (Java example)? Java中的同步辅助 - Synchronization aid in java 组合锁辅助 - Combination Lock Aid 为什么局部可变长度的for循环更快? 分支预测不会减少查找时间的影响吗? - Why are local variable length for-loops faster? Doesn't branch prediction reduce the effect of lookup times? 什么时候流优先于传统循环以获得最佳性能?流是否利用分支预测? - When should streams be preferred over traditional loops for best performance? Do streams take advantage of branch-prediction?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM