title: How can I measure the speed difference of for loop?
body: <p>I am curious about the items below in for loop.</p>
  <ol>
  <li><p>for(auto) vs for(auto &amp;)</p>
  </li>
  <li><p>Separating the for loop</p>
  </li>
  <li><p>for(auto &amp;) vs for(const auto &amp;)</p>
  </li>
  <li><p>for(int : list) vs for(auto : list) [list is integer vector]</p>
  </li>
  </ol>
  <p>So, I wrote the below code for testing in the C++17 version.</p>
  <p>It looks like seems difference in CMake debug mode(without optimization)</p>
  <pre><code>// In debug mode
  1. elapsed: 7639 (1663305922550 - 1663305914911)
  2. elapsed: 3841 (1663305926391 - 1663305922550)
  3. elapsed: 3810 (1663305930201 - 1663305926391)
  </code></pre>
  <p>But in release mode(with gcc -O3) there is no difference between 1 ~ 3</p>
  <pre><code>// release mode
  1. elapsed: 0 (1663305408984 - 1663305408984)
  2. elapsed: 0 (1663305409984 - 1663305409984)
  3. elapsed: 0 (1663305410984 - 1663305410984)
  </code></pre>
  <p>I don't know if my test method is wrong,
  Or is it correct that there is no difference depending on the optimization status?</p>
  <p>Here is my testing source code.</p>
  <pre><code>// create test vector
  const uint64_t max_ = 499999999;    // 499,999,999
  std::vector&lt;int&gt;   v;
  for (int i = 1; i &lt; max_; i++)
      v.push_back(i);
  
  
  // test 1.
  auto start1 = getTick();
  for (auto&amp; e : v)
  {
      auto t = e + 100;    t += 300;
  }
  for (auto&amp; e : v)
  {
      auto t = e + 200;    t += 300;
  }
  auto end1 = getTick();
  
  
  // test 2.
  // Omit tick function
  for (auto&amp; e : v)
  {
      auto t1 = e + 100;    t1 += 300;
      auto t2 = e + 200;    t2 += 300;
  }
  
  
  // test 3.
  for (auto e : v)
  {
      auto t1 = e + 100;    t1 += 300;
      auto t2 = e + 200;    t2 += 300;
  }
  
  ...
  
  </code></pre>
  <p>And then, getTick() was obtained through chrono milliseconds.</p>
  <pre><code>uint64_t getTick()
  {
      return (duration_cast&lt;milliseconds&gt;(system_clock::now().time_since_epoch()).count());
  }
  </code></pre>
  <p>Also, this testing progressed on Debian aarch64</p>
  <ul>
  <li>Jetson Xavier NX (jetpack 4.6, ubuntu 18.04LTS)</li>
  <li>8Gb RAM</li>
  <li>GCC 7.5.0</li>
  </ul>
  <p>Please advise if there is anything wrong. Thank you!</p>