Reputation: 2811
Mathematica has a feature called Compile
by compiling your written code in Mathematica languange you turn it into c code and run the resulting code inside Mathematica, it won't give you c code but it turns the code into it and use it. The compiled code gains a lot of speed compared to the uncompiled version.
Is there a similar feature in Matlab so that someone can easily make a code written in Matlab faster without extra effort?
It seems that Matlab compiler doesn't accomplish this goal as indeed it isn't meant to be used for this purpose. I don't know about Matlab coder. I just read something about MEX files but to me it seems as if I have to write the code in c and then bring it into Matlab for speed.
Can somebody guide me about this and show a simple example?
Upvotes: 1
Views: 918
Reputation: 3417
The short answer: compiling matlab code is not going to get you a much speed up, if any.
The longer answer: The Matlab Compiler is more for letting you deploy matlab code for use on machines which don't have matlab, rather than for getting a speed up. In fact, from poking into applications I have deployed with it, it actually wraps up the scripts with a cut down Matlab run-time. So, if anything this will slow things down due to un-packing and startup times, etc.
Matlab has some fairly good just-in-time optimization stuff in it anyway, which takes care of the low hanging fruit in terms of speed up from compilation. As horchler & chappjc mentioned in the question comments, you could try codegen
or coder
, however you may not get much better results.
If you want to try to get a speed up by using a mexFunction, then you pretty much have to re-write your function in c/c++ or fortran. Depending on the nature of the problem, your skill with C/C++ and the amount of time you have to spend on it this may end up providing a significantly quicker result as it can take advantage of your compiler's optimization tricks, and a hand coded solution may well cut out certain superfluous elements found in the matlab implementation which are necessary for generalities sake. Doing this however would break the "without significant effort" critereon.
In terms of low effort methods I would recommend you try boosting the efficiency of your matlab code first by using profile to look for bottle-necks or needless repetition, and then try to vectorize calculations wherever possible so matlab can automatically parallelize them.
If you still really need a performance boost and are working with certain matrix functions, then you could look into Matlab's GPU and CUDA support. I haven't done much with it myself yet, however it may be the case that you just have to change some datatypes to gpuArray to gain the benefit of it running on your graphics card (something of a boost for parallel computations).
Upvotes: 4