title: Is std::vector so much slower than plain arrays?
body: <p>I've always thought it's the general wisdom that <code>std::vector</code> is "implemented as an array," blah blah blah. Today I went down and tested it, and it seems to be not so:</p>
  
  <p>Here's some test results:</p>
  
  <pre><code>UseArray completed in 2.619 seconds
  UseVector completed in 9.284 seconds
  UseVectorPushBack completed in 14.669 seconds
  The whole thing completed in 26.591 seconds
  </code></pre>
  
  <p>That's about 3 - 4 times slower! Doesn't really justify for the "<code>vector</code> may be slower for a few nanosecs" comments.</p>
  
  <p>And the code I used:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  #include &lt;boost/date_time/posix_time/ptime.hpp&gt;
  #include &lt;boost/date_time/microsec_time_clock.hpp&gt;
  
  class TestTimer
  {
      public:
          TestTimer(const std::string &amp; name) : name(name),
              start(boost::date_time::microsec_clock&lt;boost::posix_time::ptime&gt;::local_time())
          {
          }
  
          ~TestTimer()
          {
              using namespace std;
              using namespace boost;
  
              posix_time::ptime now(date_time::microsec_clock&lt;posix_time::ptime&gt;::local_time());
              posix_time::time_duration d = now - start;
  
              cout &lt;&lt; name &lt;&lt; " completed in " &lt;&lt; d.total_milliseconds() / 1000.0 &lt;&lt;
                  " seconds" &lt;&lt; endl;
          }
  
      private:
          std::string name;
          boost::posix_time::ptime start;
  };
  
  struct Pixel
  {
      Pixel()
      {
      }
  
      Pixel(unsigned char r, unsigned char g, unsigned char b) : r(r), g(g), b(b)
      {
      }
  
      unsigned char r, g, b;
  };
  
  void UseVector()
  {
      TestTimer t("UseVector");
  
      for(int i = 0; i &lt; 1000; ++i)
      {
          int dimension = 999;
  
          std::vector&lt;Pixel&gt; pixels;
          pixels.resize(dimension * dimension);
  
          for(int i = 0; i &lt; dimension * dimension; ++i)
          {
              pixels[i].r = 255;
              pixels[i].g = 0;
              pixels[i].b = 0;
          }
      }
  }
  
  void UseVectorPushBack()
  {
      TestTimer t("UseVectorPushBack");
  
      for(int i = 0; i &lt; 1000; ++i)
      {
          int dimension = 999;
  
          std::vector&lt;Pixel&gt; pixels;
              pixels.reserve(dimension * dimension);
  
          for(int i = 0; i &lt; dimension * dimension; ++i)
              pixels.push_back(Pixel(255, 0, 0));
      }
  }
  
  void UseArray()
  {
      TestTimer t("UseArray");
  
      for(int i = 0; i &lt; 1000; ++i)
      {
          int dimension = 999;
  
          Pixel * pixels = (Pixel *)malloc(sizeof(Pixel) * dimension * dimension);
  
          for(int i = 0 ; i &lt; dimension * dimension; ++i)
          {
              pixels[i].r = 255;
              pixels[i].g = 0;
              pixels[i].b = 0;
          }
  
          free(pixels);
      }
  }
  
  int main()
  {
      TestTimer t1("The whole thing");
  
      UseArray();
      UseVector();
      UseVectorPushBack();
  
      return 0;
  }
  </code></pre>
  
  <p>Am I doing it wrong or something? Or have I just busted this performance myth?</p>
  
  <p>I'm using Release mode in <a href="http://en.wikipedia.org/wiki/Microsoft_Visual_Studio#Visual_Studio_2005" rel="noreferrer">Visual&nbsp;Studio&nbsp;2005</a>.</p>
  
  <hr>
  
  <p>In <a href="http://en.wikipedia.org/wiki/Visual_C%2B%2B#32-bit_versions" rel="noreferrer">Visual C++</a>, <code>#define _SECURE_SCL 0</code> reduces <code>UseVector</code> by half (bringing it down to 4 seconds). This is really huge, IMO.</p>