std::bind vs lambda performance

后端 未结 2 519
花落未央
花落未央 2020-12-06 01:18

I wanted to time a few functions\' execution and I\'ve written myself a helper:

using namespace std;
template
void         


        
2条回答
  •  情书的邮戳
    2020-12-06 02:03

    I assume that lambda cannot be that better than bind.

    That's quite a preconception.

    Lambdas are tied into the compiler internals, so extra optimization opportunities may be found. Moreover, they're designed to avoid inefficiency.

    However, there are probably no compiler optimization tricks happening here. The likely culprit is the argument to bind, bind(&decltype(result)::eval, &result). You are passing a pointer-to-member-function (PTMF) and an object. Unlike the lambda type, the PTMF does not capture what function actually gets called; it only contains the function signature (parameter and return types). The slow loop is using an indirect branch function call, because the compiler failed to resolve the function pointer through constant propagation.

    If you rename the member eval() to operator () () and get rid of bind, then the explicit object will essentially behave like the lambda and the performance difference should disappear.

提交回复
热议问题