piątek, 22 lutego 2013

Performance of summing procedures.

  1. Results
    Those results were generated with vs2012 Release configuration and full optimization:

    /c /Zi /nologo /W4 /WX- /Ox /Oy- /GL /D WIN32 /D NDEBUG /D _CONSOLE /D _UNICODE /D UNICODE /Gm- /EHsc /MD /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fo"RELEASE\\" /Fd"RELEASE\VC110.PDB" /Gd /TP /analyze-

    asyncTabIndexedSum: 1.69146 seconds (2 cores)
    tabIndexedSum: 2.37756 seconds (optimized for SSE)
    vectorIndexedSum: 3.04009 seconds
    accumulateTabSum: 3.24292 seconds
    accumulateVectorSum: 3.31494 seconds
    vectorRangedSum: 3.35942 seconds
    tabPointeredSum: 3.45808 seconds
  2. Code

    #include <tchar.h>

    #include <windows.h>
    #include <vector>
    #include <cstdlib>
    #include <iostream>
    #include <ostream>
    #include <sstream>
    #include <set>
    #include <string>
    #include <algorithm>
    #include <numeric>
    #include <future>
    using namespace std;
    size_t MAX_SIZE = (1 << 28) + 1;
    size_t MAX_REPEATS = 10;
    long long timer() {
        LARGE_INTEGER li;
        QueryPerformanceCounter(&li);
        return li.QuadPart;
    }
    long long frequency() {
        LARGE_INTEGER li;
        QueryPerformanceFrequency(&li);
        return li.QuadPart;
    }
    string format_time(const long long dt) {
        ostringstream oss;
        if ((dt) * 1000 < frequency()) {
            oss << (dt) * 1000000.0 / frequency() << " microseconds";
        } else if (dt < frequency()) {
            oss << (dt) * 1000.0 / frequency() << " milliseconds";
        } else {
            oss << (dt) * 1.0 / frequency() << " seconds";
        }
        return oss.str();
    }
    int tabIndexedSum (const int tab[], size_t tabSize)
    {
        int sum = 0;
        for (size_t i = 0; i<tabSize; ++i)
            sum += tab[i];
        return sum;
    }
    int tabPointeredSum (const int *tabBegin, const int *tabEnd)
    {
        int sum = 0;
        for (const int *i = tabBegin; i < tabEnd; ++i)
            sum += *i;
        return sum;
    }
    int vectorIndexedSum (const vector <int> &tab)
    {
        int sum = 0;
        for (size_t i=0;i<tab.size();++i)
            sum += tab[i];
        return sum;
    }
    int vectorRangedSum (const vector <int> &tab)
    {
        int sum = 0;
        for (auto i:tab)
            sum += i;
        return sum;
    }
    int accumulateVectorSum (const vector <int> &tab)
    {
        return accumulate (tab.begin(), tab.end(), 0);
    }
    int accumulateTabSum (const int tab[], size_t tabSize)
    {
        return accumulate (tab, tab + tabSize, 0);
    }
    int asyncTabIndexedSum (const int *tab, size_t tabSize)
    {
        size_t halfSize = tabSize / 2;
        auto s1 = async(launch::async, tabIndexedSum, tab, halfSize);
        auto s2 = async(launch::async, tabIndexedSum, tab +  halfSize, tabSize - halfSize);
        return s1.get() + s2.get();
    }
    int _tmain(int argc, _TCHAR argv[])
    {
        typedef pair<long long, string> tTiemrResult;
        vector <tTiemrResult> tx;
        vector <int> results;
        vector <int> tmp (MAX_SIZE);
        for (size_t i = 0; i < MAX_SIZE; ++i)
            tmp [i] = rand();
      
        tx.push_back(tTiemrResult(timer(),"start"));
        for (size_t i=0;i<MAX_REPEATS;++i)
            results.push_back(tabPointeredSum (&tmp[0], &tmp[0] + MAX_SIZE));
        tx.push_back(tTiemrResult(timer(),"tabPointeredSum"));
        for (size_t i=0;i<MAX_REPEATS;++i)
            results.push_back(tabIndexedSum (&tmp[0], MAX_SIZE));
        tx.push_back(tTiemrResult(timer(),"tabIndexedSum"));
        for (size_t i=0;i<MAX_REPEATS;++i)
            results.push_back(accumulateTabSum (&tmp[0], MAX_SIZE));
        tx.push_back(tTiemrResult(timer(),"accumulateTabSum"));
        for (size_t i=0;i<MAX_REPEATS;++i)
            results.push_back(asyncTabIndexedSum (&tmp[0], MAX_SIZE));
        tx.push_back(tTiemrResult(timer(),"asyncTabIndexedSum"));
        for (size_t i=0;i<MAX_REPEATS;++i)
            results.push_back(vectorIndexedSum (tmp));
        tx.push_back(tTiemrResult(timer(),"vectorIndexedSum"));
        for (size_t i=0;i<MAX_REPEATS;++i)
            results.push_back(vectorRangedSum (tmp));
        tx.push_back(tTiemrResult(timer(),"vectorRangedSum"));
        for (size_t i=0;i<MAX_REPEATS;++i)
            results.push_back(accumulateVectorSum (tmp));
        tx.push_back(tTiemrResult(timer(),"accumulateVectorSum"));
        cout << "resultSet: ";
        for (auto i:set <int> (results.begin(),results.end()))
             cout<< i<<", ";
        cout << endl;
        vector <tTiemrResult> tx2;
        for (size_t i=1; i < tx.size() ; i++)
            tx2.push_back(tTiemrResult(tx[i].first - tx[i-1].first, tx[i].second));
        sort (tx2.begin(), tx2.end());
        for (auto i:tx2)
            cout<<i.second<<": "<< format_time (i.first)<<endl;
        return 0;
    }

Brak komentarzy:

Prześlij komentarz