title: Matlab mex file is slow compared to its straight C equivalent
body: <p>I'm at a loss to explain (and avoid) the differences in speed between a Matlab mex program and the corresponding C program with no Matlab interface.  I've been profiling a numerical analysis program:</p>
  
  <pre><code>int main(){
  
  Well_optimized_code();
  
  }
  </code></pre>
  
  <p>compiled with gcc 4.4 against the Matlab-Mex equivalent (directed to use gcc44, which is not the version currently supported by Matlab, but it's required for other reasons):</p>
  
  <pre><code>void mexFunction(int nlhs,mxArray* plhs[], int nrhs, const mxArray* prhs[]){
  
  Well_optimized_code(); //literally the exact same code
  
  }
  </code></pre>
  
  <p>I performed the timings as:</p>
  
  <pre><code>$ time ./C_version
  </code></pre>
  
  <p>vs.</p>
  
  <pre><code>&gt;&gt; tic; mex_version(); toc
  </code></pre>
  
  <p>The difference in timing is staggering.  The version run from the command line takes 5.8 seconds on average.  The version in Matlab runs in 21 seconds.  For context, the mex file replaces an algorithm in the SimBiology toolbox that takes about 26 seconds to run.</p>
  
  <p>As compared to Matlab's algorithm, both the C and mex versions scale linearly up to 27 threads using calls to openMP, but for the purposes of profiling these calls have been disabled and commented out.</p>
  
  <p>The two versions have been compiled in the same way with the exception of the necessary flags to compile as a mex file: -fPIC --shared -lmex -DMATLAB_MEX_FILE being applied in the mex compilation/linking.  I've removed all references to the left and right arguments of the mex file.  That is to say it takes no inputs and gives no outputs, it is solely for profiling.</p>
  
  <p>The Great and Glorious Google has informed me that the position independent code should not be the source of the slowdown and beyond that I'm at a loss.</p>
  
  <p>Any help will be appreciated,</p>
  
  <p>Andrew</p>