Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Without a complete SSCCE I can't approach this the same way that I usually do with such questions.
So the best I can do is speculate:

The core difference between your two cases is that you have a branch vs. a function pointer. The fact that you are seeing a difference at all strongly hints that funcA() and funcB() are very small functions.

Possibility #1:

In the branch version of the code, funcA() and funcB() are being inlined by the compiler. Not only does that skip the function call overhead, but if the functions are trivial enough, the branch could also be completely optimized out as well.

Function pointers, on the other hand, cannot be inlined unless the compiler can resolve them at compile-time.

Possibility #2:

By comparing a branch against a function-pointer, you are putting the branch-predictorbranch-predictor against the branch target predictor.

Branch target prediction is not the same as branch prediction. In the branch case, the processor needs to predict which way to branch. In the function pointer case, it needs to predict where to branch to.

It's very likely that your processor's branch predictor is much more accurate than its branch target predictor. But then again, this is all guesswork...

Without a complete SSCCE I can't approach this the same way that I usually do with such questions.
So the best I can do is speculate:

The core difference between your two cases is that you have a branch vs. a function pointer. The fact that you are seeing a difference at all strongly hints that funcA() and funcB() are very small functions.

Possibility #1:

In the branch version of the code, funcA() and funcB() are being inlined by the compiler. Not only does that skip the function call overhead, but if the functions are trivial enough, the branch could also be completely optimized out as well.

Function pointers, on the other hand, cannot be inlined unless the compiler can resolve them at compile-time.

Possibility #2:

By comparing a branch against a function-pointer, you are putting the branch-predictor against the branch target predictor.

Branch target prediction is not the same as branch prediction. In the branch case, the processor needs to predict which way to branch. In the function pointer case, it needs to predict where to branch to.

It's very likely that your processor's branch predictor is much more accurate than its branch target predictor. But then again, this is all guesswork...

Without a complete SSCCE I can't approach this the same way that I usually do with such questions.
So the best I can do is speculate:

The core difference between your two cases is that you have a branch vs. a function pointer. The fact that you are seeing a difference at all strongly hints that funcA() and funcB() are very small functions.

Possibility #1:

In the branch version of the code, funcA() and funcB() are being inlined by the compiler. Not only does that skip the function call overhead, but if the functions are trivial enough, the branch could also be completely optimized out as well.

Function pointers, on the other hand, cannot be inlined unless the compiler can resolve them at compile-time.

Possibility #2:

By comparing a branch against a function-pointer, you are putting the branch-predictor against the branch target predictor.

Branch target prediction is not the same as branch prediction. In the branch case, the processor needs to predict which way to branch. In the function pointer case, it needs to predict where to branch to.

It's very likely that your processor's branch predictor is much more accurate than its branch target predictor. But then again, this is all guesswork...

Source Link
Mysticial
  • 469.2k
  • 46
  • 337
  • 334

Without a complete SSCCE I can't approach this the same way that I usually do with such questions.
So the best I can do is speculate:

The core difference between your two cases is that you have a branch vs. a function pointer. The fact that you are seeing a difference at all strongly hints that funcA() and funcB() are very small functions.

Possibility #1:

In the branch version of the code, funcA() and funcB() are being inlined by the compiler. Not only does that skip the function call overhead, but if the functions are trivial enough, the branch could also be completely optimized out as well.

Function pointers, on the other hand, cannot be inlined unless the compiler can resolve them at compile-time.

Possibility #2:

By comparing a branch against a function-pointer, you are putting the branch-predictor against the branch target predictor.

Branch target prediction is not the same as branch prediction. In the branch case, the processor needs to predict which way to branch. In the function pointer case, it needs to predict where to branch to.

It's very likely that your processor's branch predictor is much more accurate than its branch target predictor. But then again, this is all guesswork...