title: About unique_ptr performances
body: <p>I often read that unique_ptr would be preferred in most situations over shared_ptr because unique_ptr is non-copyable and has move semantics; shared_ptr would add an overhead due to copy and ref-counting;</p>
  
  <p>But when I test unique_ptr in some situations, it appears it's noticably slower (in access) than its counterparts </p>
  
  <p>For example, under <strong>gcc 4.5</strong> :</p>
  
  <p><strong>edit</strong> : the print method doesn't print anything actually</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;memory&gt;
  #include &lt;chrono&gt;
  #include &lt;vector&gt;
  
  class Print{
  
  public:
  void print(){}
  
  };
  
  void test()
  {
   typedef vector&lt;shared_ptr&lt;Print&gt;&gt; sh_vec;
   typedef vector&lt;unique_ptr&lt;Print&gt;&gt; u_vec;
  
   sh_vec shvec;
   u_vec  uvec;
  
   //can't use initializer_list with unique_ptr
   for (int var = 0; var &lt; 100; ++var) {
  
      shared_ptr&lt;Print&gt; p(new Print());
      shvec.push_back(p);
  
      unique_ptr&lt;Print&gt; p1(new Print());
      uvec.push_back(move(p1));
  
    }
  
   //-------------test shared_ptr-------------------------
   auto time_sh_1 = std::chrono::system_clock::now();
  
   for (auto var = 0; var &lt; 1000; ++var) 
   {
     for(auto it = shvec.begin(), end = shvec.end(); it!= end; ++it)
     {
       (*it)-&gt;print();
     }
   }
  
   auto time_sh_2 = std::chrono::system_clock::now();
  
   cout &lt;&lt;"test shared_ptr : "&lt;&lt; (time_sh_2 - time_sh_1).count() &lt;&lt; " microseconds." &lt;&lt; endl;
  
   //-------------test unique_ptr-------------------------
   auto time_u_1 = std::chrono::system_clock::now();
  
   for (auto var = 0; var &lt; 1000; ++var) 
   {
     for(auto it = uvec.begin(), end = uvec.end(); it!= end; ++it)
     {
       (*it)-&gt;print();
     }
   }
  
   auto time_u_2 = std::chrono::system_clock::now();
  
   cout &lt;&lt;"test unique_ptr : "&lt;&lt; (time_u_2 - time_u_1).count() &lt;&lt; " microseconds." &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>On average I get (g++ -O0) :</p>
  
  <ul>
  <li>shared_ptr : 1480 microseconds </li>
  <li>unique_ptr : 3350 microseconds</li>
  </ul>
  
  <p>where does the difference come from ? is it explainable ?</p>