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:

  1. Inline function

     < code> inline int fooInline (int one) {return one + 1; } Exec (fooInline);    
  2. 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 ());  
  3. std :: function (for example attached to an inline function)

      std :: The function & lt; Integer (int)> gt; FooFnc = std: bind (and fooInline); Executive (fooFnc);    
  4. Lamba

      auto fooLambda = [] (int a) {return a + 1; }; Executive (fooLambda);    
  5. 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

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -