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.

Like this post? Consider following me on Twitter or following me on Github. Don't forget to subscribe to my feed.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.18985827267169952, "perplexity": 5259.684622429487}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2023-14/segments/1679296948609.41/warc/CC-MAIN-20230327060940-20230327090940-00123.warc.gz"}