Re: [Boost-users] large data sets and BOOST_FOREACH / boost:bind don'tmix ?
I would hazard a guess that you are using MSVC with _SECURE_SCL left as the default ON. Here are my results with _SECURE_SCL on: Time: BOOST_FOREACH(Fred& fred, vec) { fred.inc(); } 1.406 Time: std::for_each(vec.begin(),vec.end(),boost::bind(&Fred::inc,_1) ); 0.515 Time: for (std::vector::iterator i = vec.begin(); i < theEnd ; i++) { (*i).inc(); } 1.313 Time: std::for_each(vec.begin();vec.end(),std::mem_fun_ref(&Fred::inc)) 0.516 Time: for (size_t i = 0; i < theSize ; i++) { vec[i].inc(); } 0.391 And with _SECURE_SCL off: Time: BOOST_FOREACH(Fred& fred, vec) { fred.inc(); } 0.36 Time: std::for_each(vec.begin(),vec.end(),boost::bind(&Fred::inc,_1) ); 0.515 Time: for (std::vector::iterator i = vec.begin(); i < theEnd ; i++) { (*i).inc(); } 0.344 Time: std::for_each(vec.begin();vec.end(),std::mem_fun_ref(&Fred::inc)) 0.516 Time: for (size_t i = 0; i < theSize ; i++) { vec[i].inc(); } 0.375 BODY { font-family:Arial, Helvetica, sans-serif;font-size:12px; }So you can see that going through a function pointer has an abstraction penalty - but this is basically the same whether it be through bind or std::mem_fun_ref (haven't checked but assume they compile to same optimized code) Iterators are faster than indexes if _SECURE_SCL is off and slower if it is on - no new news here. ForEach has a small penalty over iterators, but is rare that this matters. Pete On Fri 05/02/10 15:13 , Avi Bahra wrote: 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 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
participants (1)
-
pete@pcbartlett.com