std :: bind vs lambda performance

时间:2014-07-20 16:53:11

标签: c++ caching c++11 lambda bind

我想计算一些函数的执行时间,我自己写了一个帮手:

using namespace std;
template<int N = 1, class Fun, class... Args>
void timeExec(string name, Fun fun, Args... args) {

    auto start = chrono::steady_clock::now();

    for(int i = 0; i < N; ++i) {
        fun(args...);
    }

    auto end = chrono::steady_clock::now();

    auto diff = end - start;
    cout << name << ": "<< chrono::duration<double, milli>(diff).count() << " ms. << endl;
}

我认为对于计时成员函数,我必须使用bind或lambda,我想看看哪个会影响性能,所以我做了:

const int TIMES = 10000;
timeExec<TIMES>("Bind evaluation", bind(&decltype(result)::eval, &result));
timeExec<1>("Lambda evaluation", [&]() {
    for(int i = 0; i < TIMES; ++i) {
        result.eval();
    }
});

结果是:

Bind evaluation: 0.355158 ms.
Lambda evaluation: 0.014414 ms.

我不知道内部,但我认为lambda不能比bind更好。我能想到的唯一可信的解释是编译器优化了lambda循环中的后续函数求值。

你会如何解释它?

2 个答案:

答案 0 :(得分:14)

  

我认为lambda不能比bind更好。

这是一个非常偏见。

Lambdas与编译器内部结合,因此可以找到额外的优化机会。而且,它们旨在避免效率低下。

但是,这里可能没有编译器优化技巧。可能的罪魁祸首是绑定bind(&decltype(result)::eval, &result)的论点。您正在传递指向成员函数(PTMF)和对象的指针。与lambda类型不同,PTMF不捕获实际调用的函数;它只包含函数签名(参数和返回类型)。慢循环使用间接分支函数调用,因为编译器无法通过常量传播来解析函数指针。

如果您将成员eval()重命名为operator () ()并删除bind,则显式对象的行为基本上与lambda相同,性能差异应该消失。

答案 1 :(得分:8)

我已经测试过了。我的结果显示, Lambda实际上比绑定更快。

这是代码(请不要看风格):

#include <iostream>
#include <functional>
#include <chrono>

using namespace std;
using namespace chrono;
using namespace placeholders;

typedef void SumDataBlockEventHandler(uint8_t data[], uint16_t len);

class SpeedTest {
    uint32_t sum = 0;
    uint8_t i = 0;
    void SumDataBlock(uint8_t data[], uint16_t len) {
        for (i = 0; i < len; i++) {
            sum += data[i];
        }
    }
public:
    function<SumDataBlockEventHandler> Bind() {
        return bind(&SpeedTest::SumDataBlock, this, _1, _2);
    }
    function<SumDataBlockEventHandler> Lambda() {
        return [this](auto data, auto len)
        {
            SumDataBlock(data, len);
        };
    }
};

int main()
{
    SpeedTest test;
    function<SumDataBlockEventHandler> testF;
    uint8_t data[] = { 0,1,2,3,4,5,6,7 };

#if _DEBUG
    const uint32_t testFcallCount = 1000000;
#else
    const uint32_t testFcallCount = 100000000;
#endif
    uint32_t callsCount, whileCount = 0;
    auto begin = high_resolution_clock::now();
    auto end = begin;

    while (whileCount++ < 10) {
        testF = test.Bind();
        begin = high_resolution_clock::now();
        callsCount = 0;
        while (callsCount++ < testFcallCount)
            testF(data, 8);
        end = high_resolution_clock::now();
        cout << testFcallCount << " calls of binded function: " << duration_cast<nanoseconds>(end - begin).count() << "ns" << endl;

        testF = test.Lambda();
        begin = high_resolution_clock::now();
        callsCount = 0;
        while (callsCount++ < testFcallCount)
            testF(data, 8);
        end = high_resolution_clock::now();
        cout << testFcallCount << " calls of lambda function: " << duration_cast<nanoseconds>(end - begin).count() << "ns" << endl << endl;
    }
    system("pause");
}

控制台结果(优化发布):

100000000 calls of binded function: 1846298524ns
100000000 calls of lambda function: 1048086461ns

100000000 calls of binded function: 1259759880ns
100000000 calls of lambda function: 1032256243ns

100000000 calls of binded function: 1264817832ns
100000000 calls of lambda function: 1039052353ns

100000000 calls of binded function: 1263404007ns
100000000 calls of lambda function: 1031216018ns

100000000 calls of binded function: 1275305794ns
100000000 calls of lambda function: 1041313446ns

100000000 calls of binded function: 1256565304ns
100000000 calls of lambda function: 1031961675ns

100000000 calls of binded function: 1248132135ns
100000000 calls of lambda function: 1033890224ns

100000000 calls of binded function: 1252277130ns
100000000 calls of lambda function: 1042336736ns

100000000 calls of binded function: 1250320869ns
100000000 calls of lambda function: 1046529458ns

我已在具有完全优化(/ Ox)的发布模式下在Visual Studio Enterprise 2015下编译它,并在具有禁用优化的调试模式下编译它。结果证实lambda比笔记本电脑上的绑定速度快(戴尔Inspiron 7537,英特尔酷睿i7-4510U 2.00GHz,8GB内存)。

有人可以在您的计算机上验证这一点吗?