title: difference in CPU time for two similar lines
body: <p>There is a while loop in my program, where <code>IterZNext</code>, <code>IterZ</code> are pointers to nodes in a list. The nodes in the list are of type struct with a field called "Index".</p>
  
  <pre><code>double xx = 20.0;
  double yy = 10000.0;
  double zz;      
  while (IterZNext!=NULL &amp;&amp; NextIndex&lt;=NewIndex)
  {
      IterZ=IterZNext;
      IterZNext = IterZ-&gt;Next;
      if (IterZNext!=NULL)
      {
          zz = xx + yy;
                  NextIndex1 = IterZNext-&gt;Index; // line (*)
          NextIndex = IterZNext-&gt;Index;  // line (**)
          IterZNext-&gt;Index;
      }
  }
  </code></pre>
  
  <p>When I profiled my program, I found the line (*)</p>
  
  <pre><code>NextIndex1 = IterZNext-&gt;Index;
  </code></pre>
  
  <p>consumes most of CPU time (2.193s), while the line (**)</p>
  
  <pre><code>NextIndex = IterZNext-&gt;Index;
  </code></pre>
  
  <p>which is all most the same with the line (*) only uses 0.093s. I used the Intel VTune Amplifier to see the assembly of these two lines, which is as follows:</p>
  
  <pre><code>Address Line    Assembly                   CPU Time Instructions Retired
  Line (*):
  0x1666  561 mov eax, dword ptr [ebp-0x44]   0.015s  50,000,000
  0x1669  561 mov ecx, dword ptr [eax+0x8]        
  0x166c  561 mov dword ptr [ebp-0x68], ecx   2.178s  1,614,000,000
  
  Line (**):
  0x166f  562 mov byte ptr [ebp-0x155], 0x1   0.039s  80,000,000
  0x1676  562 mov eax, dword ptr [ebp-0x44]   0.027s  44,000,000
  0x1679  562 mov ecx, dword ptr [eax+0x8]        
  0x167c  562 mov dword ptr [ebp-0x5c], ecx   0.026s  94,000,000
  </code></pre>
  
  <p>If I change the order of the line (<em>) and the line (</em>*), then the program changes to</p>
  
  <pre><code>double xx = 20.0;
  double yy = 10000.0;
  double zz;      
  while (IterZNext!=NULL &amp;&amp; NextIndex&lt;=NewIndex)
  {
      IterZ=IterZNext;
      IterZNext = IterZ-&gt;Next;
      if (IterZNext!=NULL)
      {
          zz = xx + yy;
                  NextIndex = IterZNext-&gt;Index;  // line (**)
                  NextIndex1 = IterZNext-&gt;Index; // line (*)
          IterZNext-&gt;Index;
      }
  }
  </code></pre>
  
  <p>and the result for assembly changes to</p>
  
  <pre><code>Address Line    Assembly    CPU Time    Instructions Retired
  Line (**):
  0x1666  560 mov byte ptr [ebp-0x155], 0x1   0.044s  84,000,000
  0x166d  560 mov eax, dword ptr [ebp-0x44]   0.006s  2,000,000
  0x1670  560 mov ecx, dword ptr [eax+0x8]    0.001s  4,000,000
  0x1673  560 mov dword ptr [ebp-0x5c], ecx   1.193s  1,536,000,000
  
  Line (*):
  0x1676  561 mov eax, dword ptr [ebp-0x44]   0.052s  128,000,000
  0x1679  561 mov ecx, dword ptr [eax+0x8]        
  0x167c  561 mov dword ptr [ebp-0x68], ecx   0.034s  112,000,000
  </code></pre>
  
  <p>In this case, line (*<em>) uses most of CPU time (1.245s) while line (</em>) only uses 0.086s.</p>
  
  <p>Could someone tell me:
  (1) Why does it take so long to make the first assignment? Notice that the line zz=xx+yy only uses 0.058s. Is this related to the cache misses? since all nodes in the list are dynamically genereated.
  (2) Why is there huge difference in CPU time between this two lines?</p>
  
  <p>Thanks! </p>