CodePlea

Random thoughts on programming
10 Mar 2010

C++ Callback Benchmarks


I did a little optimization on my callback library, PlusCallback, and decided to do some benchmarks.

Speed wasn't really a consideration in PlusCallback's design; simplicity was. Still it ends up being about twice as fast as Boost.Function. I believe this is because PlusCallback takes a straightforward approach to solving the method callback problem. Boost.Function takes a much more roundabout approach, which gives it its flexibility. In fact, Boost.Function doesn't even directly support method callbacks on instantiated objects. Boost.Function method callbacks only work in combination with Boost.Bind or the standard binding functions (e.g. std::bind1st).

Here is a graph of the results, for what they're worth. The values are in millions of iterations per second, so more is better.

Boost has several intermediary steps to invoking a method callback:

0) Actual Method Being Called
1) std::mem_fun1_t::operator()
2) std::binder1st::operator()
3) boost::detail::function::function_obj_invoker1::invoke
4) boost::function1::operator()

PlusCallback really only has one behind the scenes step:

0) Actual Method Being Called
1) cb::Callback1::ChildMethod::operator()
2) cb::Callback1::operator()

Again, this reflects different design goals. PlusCallback achieves one goal directly. Boost.Function stays flexible and is more of a building block.

The benchmark code is included in the latest PlusCallback download. If you get different results, please let me know.