程序员可以使用来自 GCC 的
__builtin_expect
来显示哪些变体预计会经常出现,哪些变体很少见。但是 __builtin_expect
只有“真”和“假”(0% 或 100% 概率)
对于一些大型项目,很难获得配置文件反馈(-fprofile-arcs
),有时程序员确实知道,他在程序的某个点有多大的分支概率。
可以提示编译器一个分支的概率 >0% 和
最佳答案
来自 here :
long __builtin_expect_with_probability
(long exp, long c, double probability) The function has the same semantics as
__builtin_expect
, but caller provides the expected probability thatexp == c
. Last argument, probability, is a floating-value in the inclusive range 0.0f and 1.0f. The probability argument must be constant floating-point expression.
Jesin 在评论中指出,Clang 11 has it too .
https://stackoverflow.com/questions/4195143/