title: How macro definitions affect performance?
body: <p>I am working on a project to rotate a two dimensional array. When I use the macro to replace some codes, the results are surprising in that everything is exactly the same. The time it takes to finish the task can be significantly different. I have always thought the macro to be just a placeholder, but how could this happen?</p>
  
  <pre><code>struct pixel
  {
      unsigned short red;
      unsigned short green;
      unsigned short blue;
  } ;
  
  //n is the # of elements in the two dimensional array
  void rotate1(int n, pixel *src, pixel *dst)
  {
      int row, col, max = n - 1;
      for (row = 0; row &lt; n; row++)
          for (col = 0; col &lt; n; col++)
              *(dst+ (((max - row)*n)+col)) = *(src+ (row*n + col));
  }
  
  void rotate11(int n, pixel *src, pixel *dst)
  {
  #define TARGET  *(dst + (((max - row)*n)+col))
  #define SOURCE  *(src + (row*n + col))
  
      int row, col, max = n - 1;
      for (row = 0; row &lt; n; row++)
          for (col = 0; col &lt; n; col++)
          {
              TARGET = SOURCE;
          }
  
  }
  </code></pre>
  
  <p>The results are: </p>
  
  <pre><code>Time= 2.25 n= 512  Method= rotate1 
  Time= 2.18 n= 512  Method= rotate11 
  
  Time= 8.05 n= 1024  Method= rotate1 
  Time= 8.08 n= 1024  Method= rotate11 
  
  Time= 25.18 n= 2048  Method= rotate1 
  Time= 25.24 n= 2048  Method= rotate11 
  
  Time= 104.38 n= 4096  Method= rotate1 
  Time= 104.21 n= 4096  Method= rotate11
  
  Time= 1272.41 n= 8192  Method= rotate1 
  Time= 423.00 n= 8192  Method= rotate11
  </code></pre>
  
  <p>When n is small the two have similar performance, but as n gets larger and larger the <code>rotate11</code> takes less and less time.</p>