title: Performance difference between member function and global function in release version
body: <p>I have implemented two functions to perform the cross product of two Vectors (not std::vector), one is a member function and another is a global one, here is the key codes(additional parts are ommitted)</p>
  
  <pre><code>//for member function
  template &lt;typename Scalar&gt;
  SquareMatrix&lt;Scalar,3&gt; Vector&lt;Scalar,3&gt;::outerProduct(const Vector&lt;Scalar,3&gt; &amp;vec3) const
  {
      SquareMatrix&lt;Scalar,3&gt; result;
      for(unsigned int i = 0; i &lt; 3; ++i)
          for(unsigned int j = 0; j &lt; 3; ++j)
              result(i,j) = (*this)[i]*vec3[j];
      return result;
  }
  
  //for global function: Dim = 3
  template&lt;typename Scalar, int Dim&gt;
  void outerProduct(const Vector&lt;Scalar, Dim&gt; &amp; v1 , const Vector&lt;Scalar, Dim&gt; &amp; v2, SquareMatrix&lt;Scalar, Dim&gt; &amp; m)
  {
      for (unsigned int i=0; i&lt;Dim; i++)
          for (unsigned int j=0; j&lt;Dim; j++)
          {
              m(i,j) = v1[i]*v2[j];
          }
  }
  </code></pre>
  
  <p>They are almost the same except that one is a member function having a return value and another is a global function where the values calculated are  straightforwardly assigned to a square matrix, thus requiring no return value. <br>
  Actually, I was meant to replace the member one by the global one to improve the performance, since the first one involes copy operations. The strange thing, however, is that <strong>the time cost by the global function is almost two times longer than the member one.</strong> Furthermore, I find that the execution of</p>
  
  <pre><code>m(i,j) = v1[i]*v2[j]; // in global function
  </code></pre>
  
  <p>requires much more time than that of</p>
  
  <pre><code>result(i,j) = (*this)[i]*vec3[j]; // in member function
  </code></pre>
  
  <p>So the question is, how does this performance difference between member and global function arise? </p>
  
  <p>Anyone can tell the reasons? <br>
  Hope I have presented my question clearly, and sorry to my poor english!</p>
  
  <p>//----------------------------------------------------------------------------------------<br>
  More information added:<br>
  The following is the codes I use to test the performance:</p>
  
  <pre><code>    //the codes below is in a loop
      Vector&lt;double, 3&gt; vec1;
      Vector&lt;double, 3&gt; vec2;
      Timer timer;
      timer.startTimer();
      for (unsigned int i=0; i&lt;100000; i++)
      {
          SquareMatrix&lt;double,3&gt; m = vec1.outerProduct(vec2);
      }
      timer.stopTimer();
      std::cout&lt;&lt;"time cost for member function: "&lt;&lt; timer.getElapsedTime()&lt;&lt;std::endl;
  
      timer.startTimer();
      SquareMatrix&lt;double,3&gt; m;
      for (unsigned int i=0; i&lt;100000; i++)
      {
          outerProduct(vec1, vec2, m);
      }
      timer.stopTimer();
      std::cout&lt;&lt;"time cost for global function: "&lt;&lt; timer.getElapsedTime()&lt;&lt;std::endl;
      std::system("pause");
  </code></pre>
  
  <p>and the result captured:<br>
  <img src="https://i.stack.imgur.com/MtPLB.png" alt="enter image description here"></p>
  
  <p>You can see that the member funtion is almost twice faster than the global one.<br><br>
  Additionally, my project is built upon a 64bit windows system, and the codes are in fact used to generate the static lib files based on the Scons construction tools, along with vs2010 project files produced.</p>
  
  <p>I have to remind that <strong>the strange performance difference only occurs in a release version, while in a debug build type, the global function is almost five times faster than the member one.(about 0.10s vs 0.02s)</strong></p>