title: Why emplace_back is faster than push_back?
body: <p>I thought that <code>emplace_back</code> would be the winner, when doing something like this:</p>
  
  <pre><code>v.push_back(myClass(arg1, arg2));
  </code></pre>
  
  <p>because <code>emplace_back</code> would construct the object immediately in the vector, while <code>push_back</code>, would first construct an anonymous object and then would copy it to the vector. For more see <a href="https://stackoverflow.com/questions/13883299/c-vector-emplace-back-is-faster">this</a> question.</p>
  
  <p>Google also gives <a href="https://stackoverflow.com/questions/4303513/push-back-vs-emplace-back">this</a> and <a href="https://stackoverflow.com/questions/10890653/why-would-i-ever-use-push-back-instead-of-emplace-back">this</a> questions.</p>
  
  <p>I decided to compare them for a vector that would be filled by integers.</p>
  
  <p>Here is the experiment code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;ctime&gt;
  #include &lt;ratio&gt;
  #include &lt;chrono&gt;
  
  using namespace std;
  using namespace std::chrono;
  
  int main() {
  
    vector&lt;int&gt; v1;
  
    const size_t N = 100000000;
  
    high_resolution_clock::time_point t1 = high_resolution_clock::now();
    for(size_t i = 0; i &lt; N; ++i)
      v1.push_back(i);
    high_resolution_clock::time_point t2 = high_resolution_clock::now();
  
    duration&lt;double&gt; time_span = duration_cast&lt;duration&lt;double&gt;&gt;(t2 - t1);
  
    std::cout &lt;&lt; "push_back took me " &lt;&lt; time_span.count() &lt;&lt; " seconds.";
    std::cout &lt;&lt; std::endl;
  
    vector&lt;int&gt; v2;
  
    t1 = high_resolution_clock::now();
    for(size_t i = 0; i &lt; N; ++i)
      v2.emplace_back(i);
    t2 = high_resolution_clock::now();
    time_span = duration_cast&lt;duration&lt;double&gt;&gt;(t2 - t1);
    std::cout &lt;&lt; "emplace_back took me " &lt;&lt; time_span.count() &lt;&lt; " seconds.";
    std::cout &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>The result is that <code>emplace_back</code> is faster.</p>
  
  <pre><code>push_back took me 2.76127 seconds.
  emplace_back took me 1.99151 seconds.
  </code></pre>
  
  <p>Why? The answer of the 1st linked question clearly says that there will be no performance difference.</p>
  
  <p>Also tried with other <a href="http://gsamaras.wordpress.com/code/1651-2/" rel="noreferrer">time methods</a>, but got identical results.</p>
  
  <p>[EDIT]
  Comments say that testing with <code>int</code>s doesn't say anything and that <code>push_back</code> takes a ref.</p>
  
  <p>I did the same test in the code above, but instead of <code>int</code> I had a class <code>A</code>:</p>
  
  <pre><code>class A {
   public:
    A(int a) : a(a) {}
   private:
    int a;
  };
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>push_back took me 6.92313 seconds.
  emplace_back took me 6.1815 seconds.
  </code></pre>
  
  <p>[EDIT.2]</p>
  
  <p>As denlan said, I should also change the position of the operations, so I swapped them and in both situation (<code>int</code> and <code>class A</code>), <code>emplace_back</code> was again the winner.</p>
  
  <p>[SOLUTION]</p>
  
  <p>I was running the code in <code>debug mode</code>, which makes the measurements invalid. For benchmarking, always run the code in <code>release mode</code>.</p>