使用Lambda函数表达式,使用成员变量std::function存储Lambda函数,使用成员变量st::function存储类成员函数,分析调用性能

测试环境:windows11
测试IDE:qt creator 13.0.1 Qt:6.2.3
测试编译器:MSVC 2019 64bit
image.png

测试代码

#include <QApplication>
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;

class TestBind {
  public:
    TestBind() { m_sortCompareFunc = std::bind(&TestBind::sort, this, std::placeholders::_1, std::placeholders::_2); }
    void test(QList<int>& list) { std::stable_sort(list.begin(), list.end(), m_sortCompareFunc); }

  protected:
    virtual bool sort(const int& a, const int& b) { return false; }

  private:
    using SortCompareFunc = std::function<bool(const int&, const int&)>;
    SortCompareFunc m_sortCompareFunc;
};

class TestBindChild : public TestBind {
  protected:
    bool sort(const int& a, const int& b) { return false; }
};

class TestLambdaTemp {
  public:
    TestLambdaTemp() {}
    void test(QList<int>& list) {
        std::stable_sort(list.begin(), list.end(), [this](const int& a, const int& b) { return this->sort(a, b); });
    }

  protected:
    virtual bool sort(const int& a, const int& b) { return false; }

  private:
    using SortCompareFunc = std::function<bool(const int&, const int&)>;
    SortCompareFunc m_sortCompareFunc;
};

class TestLambdaTempChild : public TestLambdaTemp {
  protected:
    bool sort(const int& a, const int& b) { return false; }
};

class TestLambdaObj {
  public:
    TestLambdaObj() {
        m_sortCompareFunc = [this](const int& a, const int& b) { return this->sort(a, b); };
    }
    void test(QList<int>& list) { std::stable_sort(list.begin(), list.end(), m_sortCompareFunc); }

  protected:
    virtual bool sort(const int& a, const int& b) { return false; }

  private:
    using SortCompareFunc = std::function<bool(const int&, const int&)>;
    SortCompareFunc m_sortCompareFunc;
};

class TestLambdaObjChild : public TestLambdaObj {
  protected:
    bool sort(const int& a, const int& b) { return false; }
};

static QList<int> COUNT_LIST = {100, 500, 1000, 5000, 10000, 50000, 100000, 500000};
static int TEST_TIME = 10;
static QMap<int, QList<int>> BIND_CAST_MAP;
static QMap<int, QList<int>> LAMBDATEMP_CAST_MAP;
static QMap<int, QList<int>> LAMBDAOBJ_CAST_MAP;

void init() {
    for (auto& count : COUNT_LIST) {
        QList<int> list;
        for (int i = 0; i < count; ++i) {
            list.push_back(i);
        }
        BIND_CAST_MAP.insert(count, list);
        LAMBDATEMP_CAST_MAP.insert(count, list);
        LAMBDAOBJ_CAST_MAP.insert(count, list);
    }
}

int testBind(QList<int>& list) {
    auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();

    TestBindChild test;
    test.test(list);

    auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
    return (m2 - m1);
}

int testLambdaTemp(QList<int>& list) {
    auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();

    TestLambdaTempChild test;
    test.test(list);

    auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
    return (m2 - m1);
}

int testLambdaObj(QList<int>& list) {
    auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();

    TestLambdaObjChild test;
    test.test(list);

    auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
    return (m2 - m1);
}

int main(int argc, char* argv[]) {
    init();

    for (auto& count : COUNT_LIST) {
        qDebug() << "start test" << count;

        int m_testBindTotal = 0, m_testLambdaTempTotal = 0, m_testLambdaObjTotal = 0;
        for (int i = 0; i < TEST_TIME; ++i) {
            m_testBindTotal = testBind(BIND_CAST_MAP[count]);
            m_testLambdaTempTotal = testLambdaTemp(LAMBDATEMP_CAST_MAP[count]);
            m_testLambdaObjTotal = testLambdaObj(LAMBDAOBJ_CAST_MAP[count]);
        }

        qDebug() << "end test" << TEST_TIME << "aver:"
                 << "bind:" << m_testBindTotal << "lambdatemp:" << m_testLambdaTempTotal
                 << "lambdaobj:" << m_testLambdaObjTotal;
    }

    return 1;
}

测试结果

debug

image.png

release

image.png

结论

性能:Lambda函数表达式>Lambda函数std::functional成员变量>类成员函数std::functional成员变量


点墨
26 声望3 粉丝

全栈前端开发工程师