title: std::bind vs lambda performance
body: <p>I wanted to time a few functions' execution and I've written myself a helper:</p>
  
  <pre><code>using namespace std;
  template&lt;int N = 1, class Fun, class... Args&gt;
  void timeExec(string name, Fun fun, Args... args) {
  
      auto start = chrono::steady_clock::now();
  
      for(int i = 0; i &lt; N; ++i) {
          fun(args...);
      }
  
      auto end = chrono::steady_clock::now();
  
      auto diff = end - start;
      cout &lt;&lt; name &lt;&lt; ": "&lt;&lt; chrono::duration&lt;double, milli&gt;(diff).count() &lt;&lt; " ms. &lt;&lt; endl;
  }
  </code></pre>
  
  <p>I figured that for timing member functions this way I'd have to use bind or lambda and I wanted to see which would impact the performance less, so I did:</p>
  
  <pre><code>const int TIMES = 10000;
  timeExec&lt;TIMES&gt;("Bind evaluation", bind(&amp;decltype(result)::eval, &amp;result));
  timeExec&lt;1&gt;("Lambda evaluation", [&amp;]() {
      for(int i = 0; i &lt; TIMES; ++i) {
          result.eval();
      }
  });
  </code></pre>
  
  <p>The results are:</p>
  
  <pre><code>Bind evaluation: 0.355158 ms.
  Lambda evaluation: 0.014414 ms.
  </code></pre>
  
  <p>I don't know the internals, but I assume that lambda cannot be that better than bind. The only plausible explanation I can think of is the compiler optimizing-out subsequent function evaluations in the lambda's loop.</p>
  
  <p>How would you explain it?</p>