Boost logo

Boost :

Subject: Re: [boost] BOOST_FOREACH slow?
From: Hansi (hansipet_at_[hidden])
Date: 2008-11-17 13:01:51


It seems everything okay...
You are right..it is not exactly the code..

Best regards and thanks for any hint!

Hansjörg

class PerformanceCounter
{
private:
        LARGE_INTEGER CurrentTickCount_;
        LARGE_INTEGER OldTickCount_;
        LARGE_INTEGER TickFrquency_;
        double EllapsedTime_;
        double TotalTime_;
public:
        PerformanceCounter():
        EllapsedTime_(0.0),
        TotalTime_(0.0)
        {
                ::QueryPerformanceFrequency(&TickFrquency_);
                ::QueryPerformanceCounter(&CurrentTickCount_);
                OldTickCount_ = CurrentTickCount_;
        }

        void Reset(const double intial_time = 0.0)
        {
                TotalTime_ = intial_time;
                EllapsedTime_ = intial_time;
                ::QueryPerformanceCounter(&CurrentTickCount_);
                OldTickCount_ = CurrentTickCount_;
        }
        void CalculateEllapsedTime()
        {
                ::QueryPerformanceCounter(&CurrentTickCount_);
                EllapsedTime_ = (double)(CurrentTickCount_.QuadPart -
OldTickCount_.QuadPart)/TickFrquency_.QuadPart;
                TotalTime_ += EllapsedTime_;
                OldTickCount_ = CurrentTickCount_;
        }
        void CalculateEllapsedTime(const PerformanceCounter& to_intialize)
        {
                CurrentTickCount_ = to_intialize.CurrentTickCount();
                EllapsedTime_ = (double)(CurrentTickCount_.QuadPart -
OldTickCount_.QuadPart)/TickFrquency_.QuadPart;
                TotalTime_ += EllapsedTime_;
                OldTickCount_ = CurrentTickCount_;
        }
        const LARGE_INTEGER& CurrentTickCount() const {return CurrentTickCount_;}

        
        double EllapsedMilliseconds() const {return EllapsedTime_ * 1000;}
        double EllapsedSeconds() const {return EllapsedTime_;}
        double TotalTime() const {return TotalTime_;}
};

class TestData
{
public:
        TestData(int val1, int val2, int val3):var1(val1),var2(val2),var3(val3){}
        int var1;
        int var2;
        int var3;
};

int testArray[10000000];

int _tmain(int argc, _TCHAR* argv[])
{
        
        vector<int> data;
        list<int> lstData;
        vector<TestData> vectorTestData;
        list<TestData> lstTestData;

        for(int i = 0; i < 10000000;i++)
        {
                data.push_back(i);
                lstData.push_back(i);
                vectorTestData.push_back(TestData(i,i,i));
                lstTestData.push_back(TestData(i,i,i));
        }

        PerformanceCounter timer;

        int res = 0;

        const int actSize = (int)data.size();
        timer.Reset();
        for(int i = 0; i < actSize; i++)
        {
                res += data[i];
        }
        timer.CalculateEllapsedTime();
        cout<<"vector[int]: index:"<<timer.EllapsedMilliseconds()<< endl;
        
        res = 0;
        timer.Reset();
        for(vector<int>::iterator it = data.begin(); it != data.end(); it++)
        {
                res += *it;
        }
        timer.CalculateEllapsedTime();
        cout<<"vector[int]: iterator:"<<timer.EllapsedMilliseconds()<<endl;

        res = 0;
        timer.Reset();
        BOOST_FOREACH(int val,data)
        {
                res += val;
        }
        timer.CalculateEllapsedTime();
        cout<<"vector[int]:
BOOST_FOREACH:"<<timer.EllapsedMilliseconds()<<endl<<endl;

        //list
        res = 0;
        timer.Reset();
        for(list<int>::iterator it = lstData.begin(); it != lstData.end(); it++)
        {
                res += *it;
        }
        timer.CalculateEllapsedTime();
        cout<<"list[int]: iterator:"<<timer.EllapsedMilliseconds()<<endl;

        res = 0;
        timer.Reset();
        BOOST_FOREACH(int val,lstData)
        {
                res += val;
        }
        timer.CalculateEllapsedTime();
        cout<<"list[int]:
BOOST_FOREACH:"<<timer.EllapsedMilliseconds()<<endl<<endl;

        //vector TestData
        timer.Reset();
        for(int i = 0; i < actSize; i++)
        {
                res += vectorTestData[i].var1;
        }
        timer.CalculateEllapsedTime();
        cout<<"vector[int]: index:"<<timer.EllapsedMilliseconds()<< endl;
        
        res = 0;
        timer.Reset();
        for(vector<TestData>::iterator it = vectorTestData.begin(); it !=
vectorTestData.end(); it++)
        {
                res += it->var1;
        }
        timer.CalculateEllapsedTime();
        cout<<"vector[int]: iterator:"<<timer.EllapsedMilliseconds()<<endl;

        res = 0;
        timer.Reset();
        BOOST_FOREACH(TestData& val,vectorTestData)
        {
                res += val.var1;
        }
        timer.CalculateEllapsedTime();
        cout<<"vector[int]:
BOOST_FOREACH:"<<timer.EllapsedMilliseconds()<<endl<<endl;

        //list
        res = 0;
        timer.Reset();
        for(list<TestData>::iterator it = lstTestData.begin(); it !=
lstTestData.end(); it++)
        {
                res += it->var1;
        }
        timer.CalculateEllapsedTime();
        cout<<"list[TestData]: iterator:"<<timer.EllapsedMilliseconds()<<endl;

        res = 0;
        timer.Reset();
        BOOST_FOREACH(TestData& val,lstTestData)
        {
                res += val.var1;
        }
        timer.CalculateEllapsedTime();
        cout<<"list[TestData]:
BOOST_FOREACH:"<<timer.EllapsedMilliseconds()<<endl<<endl;

        timer.Reset();
        for(int i = 0; i < actSize; i++)
        {
                res += testArray[i];
        }
        timer.CalculateEllapsedTime();
        cout<<"testArray[int]: index:"<<timer.EllapsedMilliseconds()<< endl;
        
        res = 0;
        timer.Reset();
        BOOST_FOREACH(int val,testArray)
        {
                res += val;
        }
        timer.CalculateEllapsedTime();
        cout<<"testArray[int]:
BOOST_FOREACH:"<<timer.EllapsedMilliseconds()<<endl<<endl;

        return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk