title: Why using pointers (with low optimization) makes the program faster?
body: <p>I was following a tutorial on embedded C programming, then realized that using a pointer to point to variable, then using it to dereference makes the program faster!</p>
  
  <p>I've a basic knowledge of assembly, but I didn't figure why assigning the address of a variable to pointer would it make faster, we are not talking about pass by reference or by pointer or by value!</p>
  
  <p>As I can follow,</p>
  
  <ul>
  <li>code without pointer: the memory address has been assigned to register <code>R0</code>, 
  like exactly what happened in the code with the pointer.</li>
  <li>the <code>p_int</code> became an alias to the register <code>R0</code>, how this can help making the program faster?</li>
  </ul>
  
  <p>the code without using a pointer:</p>
  
  <pre><code>int counter = 0;
  int main() {
      while (counter &lt; 6) {
          ++(counter);
      }
      return 0;
  }
  </code></pre>
  
  <p>then the assembly would be like in <a href="https://i.stack.imgur.com/CrZUW.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CrZUW.jpg" alt="enter image description here"></a></p>
  
  <p>Conversely, here is the code with a pointer:</p>
  
  <pre><code>int counter = 0;
  int main() {
      int *p;
      p = &amp;counter;
      while (*p &lt; 6) {
          ++(*p);
      }
      return 0;
  }
  </code></pre>
  
  <p>then the assembly would be like in <a href="https://i.stack.imgur.com/fOywX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fOywX.jpg" alt="enter image description here"></a></p>
  
  <hr>
  
  <h2><strong>Update</strong></h2>
  
  <p>I reached out with the course creator and he was kind to replay and break it down for me, for the sake of help for others who may have encountered the same problem, I'll leave the question and the Answer </p>
  
  <blockquote>
    <p>To access a variable in memory, the CPU needs the address  of this
    variable in one of the registers. At the lowest levels of code
    optimization, the compiler  loads this address from the code memory
    before each and  every access to the variable.  The pointer speeds
    this up, because being a local variable inside the main() function is
    allocated to a register.  This means that the address sits in a
    register (R0 in this case)  and does not need to be loaded and
    re-loaded into a register each time.  At higher levels of optimization
    the compiler generates a more sensible code  and the code without the
    pointer is as fast as with the pointer. --MMS</p>
  </blockquote>