Which is faster? std::function or a C-style function pointer?

Hello,

I stumbled across this forum discussion: http://www.cplusplus.com/forum/general/181817/#msg891546

From there it seems like std::function might be even faster than a pointer. When I re-ran that test, I actually got that a pointer is faster than std::function (at least on g++). Then, I modified the test to not use `const auto` for a function instead specifying the actual type. That seemed to shave off a few more milliseconds. Also, for consistency I added a closure referenced by a pointer.

Here are the timings that I got:

----- clang++ with libc++ ---------
1
2
3
4
    raw pointer to function: 143.088 milliseconds
    raw pointer to closure: 65.177 milliseconds
wrapped pointer to function: 128.973 milliseconds
            wrapped closure: 121.94 milliseconds

----- g++ with libstdc++ ---------
1
2
3
4
    raw pointer to function: 132.545 milliseconds
    raw pointer to closure: 54.803 milliseconds
wrapped pointer to function: 157.614 milliseconds
            wrapped closure: 130.487 milliseconds


My version: http://coliru.stacked-crooked.com/a/641bcbadea692445
Original: http://coliru.stacked-crooked.com/a/73434861c73d5412

From this there is only one sure conclusion, wrapping a closure will hit performance. The rest really seems to depend on clang++ vs g++ as well as the alignment of the stars.

Are there any problems in these tests?

What would you chose if you just wanted to call a function? Without using any special features.

Thanks.
Last edited on
auto shouldn't affect runtime performance. It's a compile-time thing.

The topic that you link to is quite old so hardware and compiler version could have changed. I reran the original version and got something much closer to what you've got. http://coliru.stacked-crooked.com/a/9106a4237b45f4a6

It's difficult to write good benchmarks. Just changing the order of the tests gives you another result.

----- clang++ with libc++ ---------
1
2
3
            wrapped closure: 212.105 milliseconds
wrapped pointer to function: 131.371 milliseconds
    raw pointer to function: 66.442 milliseconds

----- g++ with libstdc++ ---------
1
2
3
            wrapped closure: 213.914 milliseconds
wrapped pointer to function: 144.063 milliseconds
    raw pointer to function: 53.169 milliseconds

http://coliru.stacked-crooked.com/a/b361eeea15baf955
Last edited on
My two cents: A few months ago I tested a complete CPU-bound application, a submodule of which needed to perform callbacks to unknown code at a relatively high frequency -- in the order of half a million times per second. The function being called back was non-trivial. I tested both function pointers+generic pointers and std::function by commenting or uncommenting a macro, so cache effects can be ignored.
I found that std::function was only marginally slower than function pointers, by around 5-10%, IIRC (considering the performance of the entire application).

My conclusion was that using std::function is worthwhile for ease of use and readability, particularly for the parts that will be called less frequently, and if you later find that you have performance problems you should profile to see if std::function is really the culprit.

PS: On second thought, the cost of std::function might have been less than 5%.
Last edited on
Topic archived. No new replies allowed.