c++ - Different ways to use a function as template argument -
I need to create a template function which again calls the "worker" function again
template & lt; Class F & gt; Int exec (f f) {long time s = 0; For (int i = 0; i <1000000; i ++) {s + = f (i); // To return to many f) return s; } Now I thought about the various possibilities of defining the worker's work:
-
Inline function
< code> inline int fooInline (int one) {return one + 1; } Exec (fooInline);
-
Work with definition from other collection unit
int fooSrc (int a); Executive (fooSrc); Function Object struct FooOp {int operator} (int a) {return a + 1; }}; Executive (FooOp ()); -
std :: function (for example attached to an inline function) std :: The function & lt; Integer (int)> gt; FooFnc = std: bind (and fooInline); Executive (fooFnc); -
Lamba
auto fooLambda = [] (int a) {return a + 1; }; Executive (fooLambda); -
temporary lambda
exec ([] (int a) {return a + 1;}); What are the differences between research methods? What will be the fastest way? Can I assume that exec (fooInline) is actually inline fooInline
There is no answer to your question within C ++ which will be one of the fastest you can measure in your particular environment, but then you have no guarantee.
It would be assumed that the compiler will have the greatest chance of inlining if it is easily accessible to the source of the worker function, so the inline function or the temporary lambda will be the best. But still a decent compiler can be inline in all the ways listed by you.
Comments
Post a Comment