title: Profile MEX - C file
body: <p>I'm new to C/C++ and MEX functions. I created the following function and I would like to optimise it (performance-wise):</p>
  
  <pre><code>#include "math.h"
  
  double GW(double i1, double i2, double h0, double C1, double C2, double C3, double m, double b, double zc, double hg)
  {
  double y0, g;
  
  g = 9.81;
  
  if (i1 &gt;= i2) {
  
      y0 = zc + h0;
  
      if (i1 - zc &lt; 0) {
          return 9999999;
      } else if ((i1 - y0 &lt; 0) &amp;&amp; (y0 &gt; zc)) {
          return 8888888;
      } else {
          double ft, Qt, fg, hp, theta, phi, psi, Qg;
  
          if ((i2-zc)/(i1-zc) &gt; 0.91+0.09*m) {
              ft = (1-(i2-zc)/(i1-zc))/((1-m)*0.3);
          } else if ((i2-zc)/(i1-zc) &gt;= m) {
              ft = pow((1-(i2-zc)/(i1-zc))/(1-m),0.5);
          } else {
              ft = 1;
          }
          Qt = 0.5*C1*b*sqrt(g)*sqrt((i1-zc)*(i1-zc)*(i1-zc))*ft;
  
          if ((i2-y0)/(i1-y0) &gt; 0.91+0.09*m) {
              fg = (1-(i2-y0)/(i1-y0))/((1-m)*0.3);
          } else if ((i2-y0)/(i1-y0) &gt;= m) {
              fg = pow((1-(i2-y0)/(i1-y0))/(1-m),0.5);
          } else {
              fg = 1;
          }
          hp = (i1-y0)/(y0 - zc);
          theta = 57.3*asin((y0-zc)/hg);
          phi = (theta-30)/60;
          if (theta &gt;= 30) {
              psi = 0.711*(1-phi)+0.58*phi*(1+0.13*hp);
          } else {
              psi = 0.711;
          }
          Qg = C2*b*pow(g,0.5)*psi*sqrt((i1-y0)*(i1-y0)*(i1-y0))*fg;
  
          if (Qt &lt; Qg) {
              return Qt;
          } else {
              return Qg;
          }
      }
  } else if (i1 &lt; i2) {
      y0 = zc + h0;
      if (i2 - zc &lt; 0) {
          return 77777;
      } else if ((i2 - y0 &lt; 0) &amp;&amp; (y0 &gt; zc)) {
          return 666666;
      } else {
          double theta, psi;
          theta = 57.3*asin((y0-zc)/hg);
          if (theta &lt; 22) {
              psi = 0.611*theta/22+0.466*(1-theta/22);
          } else {
              psi = 0.611;
          }
          if ((i2-y0)/(i1-y0) &lt; m) {
              return -C3*b*pow(g,0.5)*psi*sqrt((i2-y0)*(i2-y0)*(i2-y0));
          } else if ((i2-y0)/(i1-y0) &gt; m) {
              double fg;
              if ((i1-y0)/(i2-y0) &gt; 0.91 + 0.09*m) {
                  fg = (1-(i1-y0)/(i2-y0))/((1-m)*0.3);
              } else if ((i1-y0)/(i2-y0) &gt; m) {
                  fg = pow((1-(i1-y0)/(i2-y0))/(1-m),0.5);
              } else {
                  fg = 1;
              }
              return -fg*C3*b*pow(g,0.5)*psi*sqrt((i2-y0)*(i2-y0)*(i2-y0));
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I heard profiling in MEX is very difficult, so I created a Win32 console project using Microsoft Visual C++ 2010 Express which calls the function GW:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  
  /* Number of tests. */
   #define N 10000
  
   #define input1 35
   #define input2 34
   #define input3 2
  
   #define input4 1
   #define input5 1
   #define input6 1
  
   #define input7 1
   #define input8 4
   #define input9 30
   #define input10 8
  
  int main() {
      //std::cout &lt;&lt; "Geactiveerd!";
  int i;
  //char holdExecutionWindowOpen;
  
  //std::cin &gt;&gt; holdExecutionWindowOpen;
  
   for (i = 0; i &lt; N; ++i)
   {
       GW(input1, input2, input3, input4, input5, input6, input7, input8, input9, input10);
   }
  return 0;
  }
  </code></pre>
  
  <p>But then, what's next? Which (free) profiler is recommended for the optimization of the function GW? I tried using "Very Sleepy v0.82", but couldn't get any results. There's probably something obvious I'm doing wrong...</p>
  
  <p>Thanks in advance!</p>