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 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%.