title: clearing a vector or defining a new vector, which one is faster
body: <p>Which method is faster and has less overhead?</p>
  
  <p>Method 1:</p>
  
  <pre><code>void foo() {
    std::vector&lt; int &gt; aVector;
    for ( int i = 0; i &lt; 1000000; ++i ) {
       aVector.clear();
       aVector.push_back( i );
    }
  }
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>void foo() {
    for ( int i = 0; i &lt; 1000000; ++i ) {
       std::vector&lt; int &gt; aVector;
       aVector.push_back( i );
    }
  }
  </code></pre>
  
  <p>You may say that the example is meaningless! But this is just a snippet from my big code. In short I want to know is it better to </p>
  
  <p>"create a vector once and clear it for usage" </p>
  
  <p>or </p>
  
  <p>"create a new vector every time"</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thanks for the suggestions, I tested both and here are the results</p>
  
  <p>Method 1:</p>
  
  <pre><code> $ time ./test1
  
   real    0m0.044s
   user    0m0.042s
   sys     0m0.002s
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>$ time ./test2
  
  real    0m0.601s
  user    0m0.599s
  sys     0m0.002s
  </code></pre>
  
  <p>Clearing the vector is better. Maybe this help someone else :)</p>