title: C++ Vector vs Array (Time)
body: <p>I have got here two programs with me, both are doing exactly the same task. They are just setting an boolean array / vector to the value true. The program using vector
  takes 27 seconds to run whereas the program involving array with 5 times greater size takes less than 1 s. I would like to know the exact reason as to why there is such a major difference ? Are vectors
  really that inefficient ?</p>
  
  <p>Program using vectors</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;ctime&gt;
  
  using namespace std;
  
  int main(){
   const int size = 2000;
   time_t start, end;
   time(&amp;start);
   vector&lt;bool&gt; v(size);
   for(int i = 0; i &lt; size; i++){
    for(int j = 0; j &lt; size; j++){
     v[i] = true;
    }
   }
   time(&amp;end);
   cout&lt;&lt;difftime(end, start)&lt;&lt;" seconds."&lt;&lt;endl;
  }
  </code></pre>
  
  <p>Runtime - 27 seconds</p>
  
  <p>Program using Array</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;ctime&gt;
  
  using namespace std;
  
  int main(){
   const int size = 10000; // 5 times more size
   time_t start, end;
   time(&amp;start);
   bool v[size];
   for(int i = 0; i &lt; size; i++){
    for(int j = 0; j &lt; size; j++){
     v[i] = true;
    }
   }
   time(&amp;end);
   cout&lt;&lt;difftime(end, start)&lt;&lt;" seconds."&lt;&lt;endl;
  }
  </code></pre>
  
  <p>Runtime - &lt; 1 seconds</p>
  
  <p>Platform - Visual Studio 2008
  OS - Windows Vista 32 bit SP 1
  Processor Intel(R) Pentium(R) Dual CPU T2370 @ 1.73GHz
  Memory (RAM) 1.00 GB</p>
  
  <p>Thanks</p>
  
  <p>Amare</p>