简体   繁体   中英

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()':

likely()/unlikely() macros in the Linux kernel - how do they work? What's their benefit?

Theoretically, yes. Effectively speaking 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM