Understanding the overhead of lambda functions in C++11

前端 未结 1 2045
北荒
北荒 2021-01-30 05:20

This was already touched in Why C++ lambda is slower than ordinary function when called multiple times? and C++0x Lambda overhead But I think my example is a bit different from

相关标签:
1条回答
  • 2021-01-30 06:06

    The problem here is your usage of std::function. You send it by copy and therefore copying its contents (and doing that recursively as you unwind parameters).

    Now, for pointer to function, contents is, well, just pointer to function. For lambda, contents are at least pointer to function + reference that you captured. This is twice as much to copy. Plus, because of std::function's type erasure copying any data will most likely be slower (not inlined).

    There are several options here, and the best would probably be passing not std::function, but template instead. The benefits are that your method call is more likely to be inlined, no type erasure happens by std::function, no copying happens, everything is so very good. Like that:

    template <typename TFunc>
    void ProcessArguments(const TFunc& process)
    {}
    
    template <typename TFunc, typename HEAD, typename ... TAIL>
    void ProcessArguments(const TFunc& process, const HEAD &head, const TAIL &... tail)
    {
      process(head);
      ProcessArguments(process, tail...);
    }
    

    Second option is doing the same, but sending the process by copy. Now, copying does happen, but still is neatly inlined.

    What's equally important is that process' body can also be inlined, especially for lamda. Depending on complexity of copying the lambda object and its size, passing by copy may or may not be faster than passing by reference. It may be faster because compiler may have harder time reasoning about reference than the local copy.

    template <typename TFunc>
    void ProcessArguments(TFunc process)
    {}
    
    template <typename TFunc, typename HEAD, typename ... TAIL>
    void ProcessArguments(TFunc process, const HEAD &head, const TAIL &... tail)
    {
      process(head);
      ProcessArguments(process, tail...);
    }
    

    Third option is, well, try passing std::function<> by reference. This way you at least avoid copying, but calls will not be inlined.

    Here are some perf results (using ideones' C++11 compiler). Note that, as expected, inlined lambda body is giving you best performance:

    Original function:
    0.483035s
    
    Original lambda:
    1.94531s
    
    
    Function via template copy:
    0.094748
    
    ### Lambda via template copy:
    0.0264867s
    
    
    Function via template reference:
    0.0892594s
    
    ### Lambda via template reference:
    0.0264201s
    
    
    Function via std::function reference:
    0.0891776s
    
    Lambda via std::function reference:
    0.09s
    
    0 讨论(0)
提交回复
热议问题