title: Performance difference between multiple pointer dereferences vs references
body: <p>This was an interview question asked from me a few months back:</p>
  
  <p><strong>Which of the following functions will execute faster, <code>Foo1</code> or <code>Foo2</code>?</strong></p>
  
  <pre><code>void Foo(SomeObjectArray** array, unsigned int size)
  {
      for (int i = 0; i &lt; size; i++)
      {
          if (((*array) + i) != NULL)
          {
              ((*array) + i)-&gt;Operation1();
              ((*array) + i)-&gt;Operation2();
              ((*array) + i)-&gt;Operation3();
              ((*array) + i)-&gt;Operation4();
              ((*array) + i)-&gt;Operation5();
              ((*array) + i)-&gt;Operation6();
      }
  }
  
  void Foo(SomeObjectArray** array, unsigned int size)
  {
      for (int i = 0; i &lt; size; i++)
      {
          if (*((*array) + i) != NULL)
          {
              Object&amp; obj = *((*array) + i);
              obj.Operation1();
              obj.Operation2();
              obj.Operation3();
              obj.Operation4();
              obj.Operation5();
              obj.Operation6();
          }
      }
  }
  </code></pre>
  
  <p>Note that this is from memory so I don't remember the code exactly, but the general idea is the same. One function is using the pointer while the other uses a reference (it may have a pointer to an array like in the above code, but I don't remember exactly). I said <code>I am not sure, and will have to profile the code to find out, but if I had to guess Foo2 'may' be faster</code>. They were not impressed...</p>
  
  <p>This nagged me a couple of times here and there when I ran across code similar to this (or wrote it) and would wonder what I should do in such a case.</p>
  
  <p>I know that...</p>
  
  <ol>
  <li>This is a micro optimization</li>
  <li>The compiler will most likely optimize it</li>
  </ol>
  
  <blockquote>
    <p><strong>EDIT:</strong> I have changed the code slightly so that now it is checking for a NULL pointer. </p>
  </blockquote>