MATLAB: When to use mex files

cc-compilerMATLABmatlab codermexmex compilerspeed

I was given a lot of .m files that, together, run a single program. What files should I convert to me in order to speed it up? Is there way to run a test on the program to see where it bottlenecks? I'm new to C, Matlab, and the use of mex funcitons…

Best Answer

First thing to do is use the profiler and let MATLAB tell you where it is spending most of the time. Then you can decide on what parts of the code to speed up. Often, there are MATLAB m-code changes that can accomplish the speed up. Only after that has been vetted should mex files be considered, and even then you should have a clear idea of why the mex version of the code will be faster (avoiding data copying, etc). Depending on your version of MATLAB, the profiler is invoked differently. Look for a button at the top for "Profile" or "Run and Time" or something similar. And you can type in "doc profile" to read on on what profiling will do for you. E.g., see here:
Related Question