In my application I have very large data sets, I was using BOOST_FOREACH<br>and boost bind. But I was surprised that performance wise they were the worst ?<br>Take the following test:<br><br>class Fred {<br>public:<br>��� Fred(int i = 0) : i_(i) {}<br> ��� void inc() { i_++;}<br>private:<br>��� int i_;<br>};<br><br>BOOST_AUTO_TEST_CASE( test_loop )<br>{<br>��� size_t vecSize = 200000000;<br>��� std::vector<Fred> vec; vec.reserve(vecSize);<br>��� for (size_t i = 0; i < vecSize ; i++) { vec.push_back(Fred(i));}<br> <br>���� boost::timer timer;<br>��� BOOST_FOREACH(Fred& fred, vec) { fred.inc(); }<br>���� cout << "Time: BOOST_FOREACH(Fred& fred, vec) { fred.inc(); } " << timer.elapsed() << "\n";<br> <br>���� timer.restart();<br>���� std::for_each(vec.begin(),vec.end(),boost::bind(&Fred::inc,_1) );<br>���� cout << "Time: std::for_each(vec.begin(),vec.end(),boost::bind(&Fred::inc,_1) ); " << timer.elapsed() << "\n";<br> <br>���� timer.restart();<br>���� std::vector<Fred>::iterator theEnd = vec.end();<br>���� for (std::vector<Fred>::iterator� i = vec.begin(); i < theEnd ; i++) { (*i).inc(); }<br>���� cout << "Time: for (std::vector<Fred>::iterator� i = vec.begin(); i < theEnd ; i++) { (*i).inc(); } " << timer.elapsed() << "\n";<br> <br>���� timer.restart();<br>���� std::for_each(vec.begin(),vec.end(),std::mem_fun_ref(&Fred::inc) );<br>���� cout << "Time: std::for_each(vec.begin();vec.end(),std::mem_fun_ref(&Fred::inc)) " << timer.elapsed() << "\n";<br> <br>���� timer.restart();<br>���� size_t theSize = vec.size();<br>��� for (size_t i = 0; i < theSize ; i++) { vec[i].inc(); }<br>���� cout << "Time: for (size_t i = 0; i < theSize ; i++) { vec[i].inc(); } " << timer.elapsed() << "\n";<br> }<br><br>I get following times when run on suse linux with gcc 4.2.1/ boost 1.39<br><br>Time: BOOST_FOREACH(Fred& fred, vec) { fred.inc(); }����������������������������������������� � �� 10.65<br>Time: std::for_each(vec.begin(),vec.end(),boost::bind(&Fred::inc,_1) ); � � � � � � � � � � �� 9.76<br> Time: for (std::vector<Fred>::iterator� i = vec.begin(); i < theEnd ; i++) { (*i).inc(); }� 4.48<br>Time: std::for_each(vec.begin();vec.end(),std::mem_fun_ref(&Fred::inc))�������������������� 4.08<br>Time: for (size_t i = 0; i < theSize ; i++) { vec[i].inc(); }����������������������������������������������������� 1.6<br> <br>It appears that BOOST_FOREACH and use of boost::bind in performance critical<br>application should be avoided ?<br>-- <br> � Best regards,<br>Ta,<br> � �Avi<br><br>