title: Performance swapping integers vs double
body: <p>For some reason my code is able to perform swaps on doubles faster than on the integers. I have no idea why this would be happening.</p>
  
  <p>On my machine the double swap loop completes 11 times faster than the integer swap loop. What property of doubles/integers make them perform this way?</p>
  
  <p><strong>Test setup</strong></p>
  
  <ul>
  <li>Visual Studio 2012 x64</li>
  <li>cpu core i7 950</li>
  <li><strong>Build as Release and run exe directly, VS Debug hooks skew things</strong></li>
  </ul>
  
  <p><strong>Output:</strong></p>
  
  <p><code>Process time for ints 1.438 secs</code></p>
  
  <p><code>Process time for doubles  0.125 secs</code></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;ctime&gt;
  using namespace std;
  
  #define N 2000000000
  
  void swap_i(int *x, int *y) {
      int tmp = *x;
      *x = *y;
      *y = tmp;
  }
  
  void swap_d(double *x, double *y) {
      double tmp = *x;
      *x = *y;
      *y = tmp;
  }
  
  int main () {
      int a = 1, b = 2;
      double d = 1.0, e = 2.0, iTime, dTime;
      clock_t c0, c1;
  
      // Time int swaps
      c0 = clock();
      for (int i = 0; i &lt; N; i++) {
          swap_i(&amp;a, &amp;b);
      }
      c1 = clock();
      iTime = (double)(c1-c0)/CLOCKS_PER_SEC;
  
      // Time double swaps
      c0 = clock();
      for (int i = 0; i &lt; N; i++) {
          swap_d(&amp;d, &amp;e);
      }
      c1 = clock();
      dTime = (double)(c1-c0)/CLOCKS_PER_SEC;
  
      cout &lt;&lt; "Process time for ints " &lt;&lt; iTime &lt;&lt; " secs" &lt;&lt; endl;
      cout &lt;&lt; "Process time for doubles  " &lt;&lt; dTime &lt;&lt; " secs" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>It seems that VS only optimized one of the loops as Blastfurnace explained. </p>
  
  <p>When I disable all compiler optimizations and have my swap code inline inside the loops, I got the following results (I also switched my timer to std::chrono::high_resolution_clock):</p>
  
  <p><code>Process time for ints 1449 ms</code></p>
  
  <p><code>Process time for doubles  1248 ms</code></p>