
Dominique Devienne:
Probably a silly question, but has anyone performed benchmarks on using a std::for_each(begin, end, bind(&C::f, _1)); compared to the hand written loop? Under the debugger I see bind adds a 4 or 5 stack frames between the for_each frame and the C::f frame, so I'm wondering if there is any overhead in these?
It depends (heavily) on the amount of inlining the compiler does. In the simple case of #include <boost/bind.hpp> #include <algorithm> struct X { void f(); }; int main() { extern int N; extern X x[]; std::for_each( x, x + N, boost::bind( &X::f, _1 ) ); } the loop generated by MSVC 7.1 in a release build is $L9292: mov ecx, esi call ebx inc esi cmp esi, edi jne SHORT $L9292 where ebx points at X::f. More complicated examples would probably carry more overhead as the compiler inlining threshold is reached.