title: c++ local vs member variable performance
body: <p>The intention of the question is efficeincey a good reason to use local variable than member , I am not trying to test compiler optimization here ... Please </p>
  
  <p>If you think my comparision isn't perfect , kindly provide an alternative code.</p>
  
  <p>Any one can explain in detail why ?
  Why Local acess is faster even if it has to crteate stack and dismantle stack everytime it calls the function 
  While member it just has to derefrence this pointer 
  I can see local stack is faster ?</p>
  
  <hr>
  
  <p>RESULT
  local time:271
  member time:418</p>
  
  <p>Code :</p>
  
  <pre><code>class local {
  public:
   void incr() {
          int i;
          ++i;
  }
  
  
  };
  
  
  class Member {
  int i;
  
  public:
   void incr() {
          ++i;
  }
  
  };
  
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;time.h&gt;
  int main(int argc, char**argv) {
  
          time_t star;
          time_t end;
          Member m;
          local l;
          time(&amp;star);
          for(unsigned int j=0;j&lt;200000;++j)
          for(unsigned int i=0;i&lt;400000;++i) {
                  l.incr();
          }
          time(&amp;end);
          std::cout &lt;&lt; "\nlocal time:" &lt;&lt; end-star &lt;&lt; "\n";
  
          time(&amp;star);
          for(unsigned int j=0;j&lt;200000;++j)
          for(unsigned int i=0;i&lt;400000;++i) {
                  m.incr();
          }
          time(&amp;end);
          std::cout &lt;&lt; "\nmember time:" &lt;&lt; end-star&lt;&lt; "\n";
  return 0;
  }
  </code></pre>
  
  <p>NEW CODE:
  g++  -O1 localmember.cpp </p>
  
  <p>local time:128
  member time:117</p>
  
  <p>Code :</p>
  
  <pre><code>class local {
  public:
   int diff(int a, int b) {
          int d=0;
          d=a-b;
          return d;
  }
  };
  
  
  class Member {
  int d;
  
  public:
   int diff(int a, int b) {
          d=0;
          d=a-b;
          return d;
  }
  
  };
  
  static int gr;
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;time.h&gt;
  void dumpdiff(int r) {
  gr=r ;
  }
  int main(int argc, char**argv) {
  
          time_t star;
          time_t end;
          Member m;
          local l;
          int r=0;
          int r2=0;
          time(&amp;star);
          int in1=2,in2=0;
          for(unsigned int j=0;j&lt;200000;++j)
          for(unsigned int i=0;i&lt;200000;++i) {
                  r = l.diff(in1*i,in2*i);
                  in1+=1;
                  in2+=1;
                  if(r){r2=r;}
                  dumpdiff(r);
          }
          time(&amp;end);
          std::cout &lt;&lt; "\nlocal time:" &lt;&lt; end-star &lt;&lt; "\n";
  
          time(&amp;star);
          for(unsigned int j=0;j&lt;200000;++j)
          for(unsigned int i=0;i&lt;200000;++i) {
                  r = m.diff(in1,in2);
                  in1+=1;
                  in2+=1;
                  if(r){r2=r;}
                  dumpdiff(r);
          }
          time(&amp;end);
          std::cout &lt;&lt; "\nmember time:" &lt;&lt; end-star&lt;&lt; "\n";
  return 0;
  </code></pre>