std::bind vs lambda performance

后端 未结 2 512
花落未央
花落未央 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:04

    I've tested it. My results shows, that Lambda is actually faster than bind.

    This is the code (please don't look at style):

    #include 
    #include 
    #include 
    
    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 Bind() {
            return bind(&SpeedTest::SumDataBlock, this, _1, _2);
        }
        function Lambda() {
            return [this](auto data, auto len)
            {
                SumDataBlock(data, len);
            };
        }
    };
    
    int main()
    {
        SpeedTest test;
        function 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(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(end - begin).count() << "ns" << endl << endl;
        }
        system("pause");
    }
    

    Console results (Release with optimalization):

    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
    

    I've compiled it under Visual Studio Enterprise 2015 in the Release mode with Full Optimization (/ Ox) and in the Debug mode with disabled optimalization. Results confirm that lambda is faster than the bind on my laptop (Dell Inspiron 7537, Intel Core i7-4510U 2.00GHz, 8GB RAM).

    Can anyone verify this on your computer?

提交回复
热议问题