Boost logo

Boost :

Subject: [boost] BOOST_FOREACH slow?
From: Hansi (hansipet_at_[hidden])
Date: 2008-11-17 12:10:09


Hello,

I have made a few tests with the performance of BOOST_FOREACH. The
results are for me unexpected slow...may be one has an idea if I make
something wrong...

vector[int]: index:0.00167619
vector[int]: iterator:0.00167619
vector[int]: BOOST_FOREACH:9.40036

list[int]: iterator:239.669
list[int]: BOOST_FOREACH:233.219

vector[TestData]: index:0.00167619
vector[TestData]: iterator:0.00167619
vector[TestData]: BOOST_FOREACH:9.40008

list[TestData]: iterator:233.827
list[TestData]: BOOST_FOREACH:238.513

testArray[int]: index:0.00167619
testArray[int]: BOOST_FOREACH:9.40008

Best regards
Hansjörg

the test program was:

// ForEach.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "..\include\PerformanceCounter.h"
#include <vector>
#include <boost\foreach.hpp>
#include <iostream>
#include <list>

using namespace std;
using namespace Microtec;

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));
        }

        int res = 0;

        const int actSize = (int)data.size();
        for(int i = 0; i < actSize; i++)
        {
                res += data[i];
        }
        //takes 0.0017 ms
        
        res = 0;
        for(vector<int>::iterator it = data.begin(); it != data.end(); it++)
        {
                res += *it;
        }
        //takes 0.0017 ms

        res = 0;
        BOOST_FOREACH(int val,data)
        {
                res += val;
        }
        //takes 9.400 ms

        //list
        res = 0;
        for(list<int>::iterator it = lstData.begin(); it != lstData.end(); it++)
        {
                res += *it;
        }
        //takes 239.669 ms

        res = 0;
        BOOST_FOREACH(int val,lstData)
        {
                res += val;
        }
        //takes 233.219 ms

        //vector TestData
        for(int i = 0; i < actSize; i++)
        {
                res += vectorTestData[i].var1;
        }
        //takes 0.0017 ms
        
        res = 0;
        for(vector<TestData>::iterator it = vectorTestData.begin(); it !=
vectorTestData.end(); it++)
        {
                res += it->var1;
        }
        //takes 0.0017 ms

        res = 0;
        BOOST_FOREACH(TestData& val,vectorTestData)
        {
                res += val.var1;
        }
        //takes 9.400 ms

        //list
        res = 0;
        for(list<TestData>::iterator it = lstTestData.begin(); it !=
lstTestData.end(); it++)
        {
                res += it->var1;
        }
        //takes 233.827 ms

        res = 0;
        BOOST_FOREACH(TestData& val,lstTestData)
        {
                res += val.var1;
        }
        //takes 238.827 ms

        for(int i = 0; i < actSize; i++)
        {
                res += testArray[i];
        }
        //takes 0.0017 ms
        
        res = 0;
        BOOST_FOREACH(int val,testArray)
        {
                res += val;
        }
        //takes 9.400 ms

        return 0;
}


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