title: Why is an array so much faster than a vector?
body: <p>Is this a fair test for comparing a vector with an array? The difference in speed seems too large. My test suggests the array is 10 to 100 times faster!</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;windows.h&gt;
  #include &lt;stdint.h&gt;
  
  using namespace std;
  
  double PCFreq = 0.0;
  __int64 CounterStart = 0;
  
  using namespace std;
  
  void StartCounter()
  {
      LARGE_INTEGER li;
      if(!QueryPerformanceFrequency(&amp;li))
      std:cout &lt;&lt; "QueryPerformanceFrequency failed!\n";
  
      PCFreq = double(li.QuadPart)/1000000000;
  
      QueryPerformanceCounter(&amp;li);
      CounterStart = li.QuadPart;
  }
  double GetCounter()
  {
      LARGE_INTEGER li;
      QueryPerformanceCounter(&amp;li);
      return double(li.QuadPart-CounterStart)/PCFreq;
  }
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      //Can do 100,000 but not 1,000,000
      const int vectorsize = 100000;
      cout.precision(10);
  
      StartCounter();
      vector&lt;int&gt; test1(vectorsize);
      for(int i=0; i&lt;vectorsize; i++){
          test1[i] = 5;
      }
      cout &lt;&lt; GetCounter() &lt;&lt; endl &lt;&lt; endl;
  
  
      StartCounter();
      int test2[vectorsize];
      for(int i=0; i&lt;vectorsize; i++){
          test2[i] = 5;
      }
      cout &lt;&lt; GetCounter() &lt;&lt; endl &lt;&lt; endl;
  
      cout &lt;&lt; test2[0];
  
      int t = 0;
      cin &gt;&gt; t;
      return 0;
  }
  </code></pre>