Reputation: 35
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?
Upvotes: 3
Views: 436
Reputation: 5326
If you are using GCC you can use the macros likely()/unlikely()
.
Upvotes: 7
Reputation: 129
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.
Upvotes: 1