title: performance comparsion between vector and raw c-style array
body: <p>I have profiled the performance between c++ vector and c-style array.  The result is a little bit unexpected since the literature says the performance of vector should come very close to raw array, but it does not.  Did I do anything wrong in my profiling? </p>
  
  <pre><code>void getVector1(int n)
  {
      if (n &lt; 0)
      {
          throw std::invalid_argument(std::string("negative argument n:") + std::to_string(n));
      }
  
      auto tp1 = std::chrono::steady_clock::now();
      std::vector&lt;int&gt; ivec(n);
      int i = 0;
      for (auto&amp; x : ivec)
      {
          x = ++i;
      }
  
      auto tp2 = std::chrono::steady_clock::now();
      std::chrono::duration&lt;double, std::micro&gt; dd = tp2 - tp1;
  
      printf("spend %6.2f us time to create: %d elements vector inside %s() at %s:%d \n", dd.count(), n, __func__, __FILE__, __LINE__);
  }
  
  
  void getVector2(int n)
  {
      if (n &lt; 0)
      {
          throw std::invalid_argument(std::string("negative argument n:") + std::to_string(n));
      }
  
      auto tp1 = std::chrono::steady_clock::now();
      auto pvec = new int[n];
  
      for (int i = 0; i &lt; n; ++i)
      {
          pvec[i] = i;
      }
  
      auto tp2 = std::chrono::steady_clock::now();
      std::chrono::duration&lt;double, std::micro&gt; dd = tp2 - tp1;
  
      delete[] pvec;
      printf("spend %6.2f us time to create: %d elements vector inside %s() at %s:%d \n", dd.count(), n, __func__, __FILE__, __LINE__);
  }
  
  
  
  int main()
  {
      int n = 10000000;
      getVector1(n);
      getVector2(n);
  
      return 0;
  }
  </code></pre>
  
  <p>The code  was compiled using g++ with -O3 option.</p>
  
  <p>spend 11946.38 us time to create: 10000000 elements vector inside getVector1() at testVectorSpeed.cpp</p>
  
  <p>spend 7298.66 us time to create: 10000000 elements vector inside getVector2() at testVectorSpeed.cpp</p>