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