Kar*_*rol 9 performance gcc branch if-statement prediction
例:
if (almost_always_false_condition) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
有没有办法建议编译器在99%的条件下将是假的.条件计算需要大约60个周期进行检查,编译器本身无法在编译时计算.
(gcc 4.3)
Ste*_*non 10
如果您想向GCC建议条件可能具有给定值作为安排代码流的提示,您应该使用__builtin_expect( ):
if (__builtin_expect(almost_always_false_condition,0)) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
但是,听起来你想找到一种方法来避免评估条件,这是__builtin_expect( )不可能的.有没有一种方法可以快速逼近条件,只有在逼近时才进行全面检查:
if (__builtin_expect(fastCheckThatIsTrueIfFullConditionIsTrue,0)) {
// most of the time, we don't even get to here, so you don't need
// to evaluate the condition
if (almostAlwaysFalseCondition) {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
你能告诉我们更多的情况吗?