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.

4 Responses to “C++ Callback Benchmarks”

  1. bubu Says:

    Could you add Impossibly Fast C++ Delegates to your benchmark: http://www.codeproject.com/KB/cpp/ImpossiblyFastCppDelegate.aspx ?

  2. Damn3d Says:

    Extremely useful library, IMHO much better than the bloated libsigc++. However it still would be nice to see libsigc++ in your performance comparison ;)

  3. neo73 Says:

    Thanks for sharing this very useful library and benchmark script. I experienced different results using g++ 4.6 and I benchmarked Elbert Mai’s CPPCallback and libsigc++ callback implementations. The results and updated benchmark script can be found at http://www.rooot.net/en/development/code/38-cpp-member-function-pointers-and-delegates.html
    What compiler did you used to run your benchmark?

  4. Lewis Says:

    I’m using g++ on Windows (MinGW). I just ran your updated benchmark with CPPCallback and had very similar results to what you got.

    My primary design goal with PlusCallback was to make something simple and easy to use. Being fast was an accidental side effect of that, so I’m okay if it’s not the fastest.

    CPPCallback looks pretty good. If it was around a couple years ago, I might not have made PlusCallback.

Leave a Reply