title: C++ smart pointer performance and difference with a simple wrapped pointer
body: <p>I came across this test that someone did on C++ smart pointers, and I was wondering a couple of things. First of all, I've heard that make_shared and make_unique are faster than normal construction of a shared or unique pointer. But my results and the results of the guy who created the test showed that make_unique and make_shared are slightly slower (probably nothing significant). But I was also wondering, in debug mode for me a unique_pointer is about 3 times slower than normal pointers, and indeed also much slower than a simply wrapping a pointer in a class myself. In release mode the raw pointers, my wrapped class and unique_ptrs were roughly the same. I was wondering, does the unique_pointer do anything special that I would lose if I used my own smart pointer? It seems to be rather heavy, well at least in debug mode it seems to be doing a lot. The test is below:</p>
  
  <pre><code>#include &lt;chrono&gt;
  #include &lt;iostream&gt;
  #include &lt;memory&gt;
  
  static const long long numInt = 100000000;
  
  template &lt;typename T&gt;
  struct SmartPointer
  {
      SmartPointer(T* pointee) : ptr(pointee) {}
      T* ptr;
      ~SmartPointer() { delete ptr; }
  };
  
  int main() {
  
      auto start = std::chrono::system_clock::now();
  
      for (long long i = 0; i &lt; numInt; ++i) {
          //int* tmp(new int(i));
          //delete tmp;
          //SmartPointer&lt;int&gt; tmp(new int(i));
          //std::shared_ptr&lt;int&gt; tmp(new int(i));
          //std::shared_ptr&lt;int&gt; tmp(std::make_shared&lt;int&gt;(i));
          //std::unique_ptr&lt;int&gt; tmp(new int(i));
          //std::unique_ptr&lt;int&gt; tmp(std::make_unique&lt;int&gt;(i));
      }
  
      std::chrono::duration&lt;double&gt; dur = std::chrono::system_clock::now() - start;
      std::cout &lt;&lt; "time native: " &lt;&lt; dur.count() &lt;&lt; " seconds" &lt;&lt; std::endl;
  
      system("pause");
  }
  </code></pre>
  
  <p>The link where I found this is at 
  <a href="http://www.modernescpp.com/index.php/memory-and-performance-overhead-of-smart-pointer" rel="nofollow noreferrer">http://www.modernescpp.com/index.php/memory-and-performance-overhead-of-smart-pointer</a></p>