title: why the large difference, between for loops
body: <p>I got curious with the difference between a <code>for(;;)</code> and <code>for(:)</code>, 
  particularly with the speed between the two. So I ran a little test by having a vector of 10 million integers and adding them all together in a for. I found that the <code>for(:)</code> was 1.3 slower.</p>
  
  <p>What would cause the <code>for(:)</code> to be that much slower!?</p>
  
  <p>EDIT:
  It seems like the for(:) uses the iterator of the vector
  unlike the for(;;) making it longer.</p>
  
  <blockquote>
    <p>/Yu"stdafx.h" /GS /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /sdl /Fd"Debug\vc120.pdb" /fp:precise /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\forvsForLoop.pch" </p>
  </blockquote>
  
  <pre><code>#include "stdafx.h"
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;chrono&gt;
  
  void init(std::vector&lt;int&gt; &amp;array){
      srand(20);
      for (int x = 0; x &lt; 10000000; x++)
          array.push_back(rand());
      return;
  }
  
  unsigned long testForLoop(std::vector&lt;int&gt; &amp;array){
      unsigned long result = 0;
      for (int x = 0; x &lt; array.size(); x++)
          result += array[x];
      return result;
  }
  unsigned long testFor(std::vector&lt;int&gt; &amp;array){
      unsigned long result = 0;
      for (const int &amp;element : array)
          result += element;
      return result;
  }
  int _tmain(int argc, _TCHAR* argv[])
  {
      std::vector&lt;int&gt; testingArray;
  
      init(testingArray);
  
      //Warm up
      std::cout &lt;&lt; "warming up \n";
      testForLoop(testingArray);
      testFor(testingArray);
      testForLoop(testingArray);
      testFor(testingArray);
      testForLoop(testingArray);
      testFor(testingArray);
      std::cout &lt;&lt; "starting \n";
  
      auto start = std::chrono::high_resolution_clock::now();
      testForLoop(testingArray);
      auto end = std::chrono::high_resolution_clock::now();
      std::cout &lt;&lt; "ForLoop took: " &lt;&lt;  std::chrono::duration_cast&lt;std::chrono::nanoseconds&gt;(end - start).count() &lt;&lt; std::endl;
  
  
      start = std::chrono::high_resolution_clock::now();
      testFor(testingArray);
      end = std::chrono::high_resolution_clock::now();
      std::cout &lt;&lt; "For---- took: " &lt;&lt; std::chrono::duration_cast&lt;std::chrono::nanoseconds&gt;(end - start).count() &lt;&lt; std::endl;
  
      system("pause");
      return 0;
  
  }
  </code></pre>