Are functors actually faster than pointers to functions?

前端 未结 3 1049
刺人心
刺人心 2021-01-31 06:58

According to Scott Meyers, one area where C++ shines over C is that function objects are faster than function pointers. He says this is because function objects are inlined, whi

3条回答
  •  野性不改
    2021-01-31 07:42

    Yes, function objects might lead to faster code. But the only way to ensure that is to benchmark.

    1. The documentation says: "GCC may still be unable to inline a function for many reasons; the -Winline option may be used to determine if a function has not been inlined and why not."

    2. Of course it will depend on compiler, version, flags, etc. Sometimes inlining can be counter-productive (code bloat etc.), so each compiler has its own set of rules to decide whether a function should be inlined. By the way, the inline keyword is only a hint, and some libraries such as eigen have a hard time to enforce inlining.

提交回复
热议问题