title: Why would one CPU core run slower than the others?
body: <p>I was benchmarking a large scientific application, and found it would sometimes run 10% slower given the same inputs.  After much searching, I found the the slowdown only occurred when it was running on core #2 of my quad core CPU (specifically, an Intel Q6600 running at 2.4 GHz).  The application is a single-threaded and spends most of its time in CPU-intensive matrix math routines.</p>
  
  <p>Now that I know one core is slower than the others, I can get accurate benchmark results by setting the processor affinity to the same core for all runs.  However, I still want to know why one core is slower.</p>
  
  <p>I tried several simple test cases to determine the slow part of the CPU, but the test cases ran with identical times, even on slow core #2.  Only the complex application showed the slowdown.  Here are the test cases that I tried:</p>
  
  <ul>
  <li><p>Floating point multiplication and addition:</p>
  
  <pre><code>accumulator = accumulator*1.000001 + 0.0001;
  </code></pre></li>
  <li><p>Trigonometric functions:</p>
  
  <pre><code>accumulator = sin(accumulator);
  accumulator = cos(accumulator);
  </code></pre></li>
  <li><p>Integer addition:</p>
  
  <pre><code>accumulator = accumulator + 1;
  </code></pre></li>
  <li><p>Memory copy while trying to make the L2 cache miss:</p>
  
  <pre><code>int stride = 4*1024*1024 + 37;  // L2 cache size + small prime number
  for(long iter=0; iter&lt;iterations; ++iter) {
      for(int offset=0; offset&lt;stride; ++offset) {
          for(i=offset; i&lt;array_size; i += stride) {
              array1[i] = array2[i];
          }
      }
  }
  </code></pre></li>
  </ul>
  
  <p><strong>The Question:</strong> Why would one CPU core be slower than the others, and what part of the CPU is causing that slowdown?</p>
  
  <p><strong>EDIT:</strong> More testing showed some <a href="http://en.wikipedia.org/wiki/Unusual_software_bug#Heisenbug" rel="noreferrer">Heisenbug</a> behavior.  When I explicitly set the processor affinity, then my application does not slow down on core #2.  However, if it chooses to run on core #2 without an explicitly set processor affinity, then the application runs about 10% slower.  That explains why my simple test cases did not show the same slowdown, as they all explicitly set the processor affinity.  So, it looks like there is some process that likes to live on core #2, but it gets out of the way if the processor affinity is set.</p>
  
  <p><em>Bottom Line:</em> If you need to have an accurate benchmark of a single-threaded program on a multicore machine, then make sure to set the processor affinity.</p>