title: Matlab mex-file with mexCallMATLAB is almost 300 times slower than the corresponding m-file
body: <p>I started implementing a few m-files in C++ in order to reduce run times.  The m-files produce n-dimensional points and evaluate function values at these points. The functions are user-defined and they are passed to m-files and mex-files as function handles. The mex-files use mexCallMATLAB with feval for finding function values.</p>
  
  <p>I constructed the below example where a function handle fn constructed in the Matlab command line is passed to matlabcallingmatlab.m and mexcallingmatlab.cpp routines. With a freshly opened Matlab, mexcallingmatlab evaluates this function 200000 in 241.5 seconds while matlabcallingmatlab evaluates it in 0.81522 seconds therefore a 296 times slow-down with the mex implementation. These times are the results of the second runs as the first runs seem to be larger probably due to some overhead associated first time loading the program etc.</p>
  
  <p>I have spent many days searching online on this problem and tried some suggestions on it. I tried different mex compiling flags to optimize the mex but there was almost no difference in performance. A previous post in Stackoverflow stated that upgrading Matlab was the solution but I am using probably the latest version MATLAB Version: 8.1.0.604 (R2013a) on Mac OS X  Version: 10.8.4. I did compile the mex file with and without –largeArrayDims flag but this didn’t make any difference either. Some suggested that the content of the function handle could be  directly coded in the cpp file but this is impossible as I would like to provide this code to any user with any type of function with a vector input and real number output.</p>
  
  <p>As far as I found out, mex files need to go through feval function for using a function handle whereas m-files can directly call function handles provided that Matlab version is newer than some version.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p><strong>simple function handle created in the Matlab command line</strong>:</p>
  
  <pre><code>fn = @(x) x'*x 
  </code></pre>
  
  <p><strong>matlabcallingmatlab.m</strong> :</p>
  
  <pre><code>function matlabcallingmatlab( fn )
  x = zeros(2,1); 
  for i = 0 : 199999
      x(2) = i; 
      f = fn( x ); 
  end
  </code></pre>
  
  <p><strong>mexcallingmatlab.cpp</strong>:</p>
  
  <pre><code>#include "mex.h"
  #include &lt;cstring&gt;
  
  void mexFunction( int nlhs, mxArray *plhs[],
                    int nrhs, const mxArray *prhs[] )
  {
      mxArray *lhs[1], *rhs[2]; //parameters to be passed to feval
      double f, *xptr, x[] = {0.0, 0.0}; // x: input to f and f=f(x)
      int n = 2, nbytes = n * sizeof(double);  // n: dimension of input x to f
  
      // prhs[0] is the function handle as first argument to feval
      rhs[0] = const_cast&lt;mxArray *&gt;( prhs[0] );
  
      // rhs[1] contains input x to the function
      rhs[1] = mxCreateDoubleMatrix( n, 1, mxREAL);
      xptr = mxGetPr( rhs[1] );
  
      for (int i = 0; i &lt; 200000; ++i)
      {
          x[1] = double(i);   // change input 
          memcpy( xptr, x, nbytes );  // now rhs[1] has new x
          mexCallMATLAB(1, lhs, 2, rhs, "feval");
          f = *mxGetPr( lhs[0] );
      }
  }
  </code></pre>
  
  <p><strong>Compilation of mex file</strong>:</p>
  
  <pre><code>&gt;&gt; mex -v -largeArrayDims mexcallingmatlab.cpp
  </code></pre>