speed up m-file with mex

I find many example of mex, but all of examples I found are that all of the methods to write code just like C code and without calling any matlab bulit-in function. Does m-file can not be transformed to mex file? I tried to do this, but it present the result that is not successful.

2 commentaires

Jan
Jan le 18 Août 2015
Modifié(e) : Jan le 18 Août 2015
Plöease explain any details. What did you try and what exactly does "not successful" mean? What is the connection to the tag "P-file"? If there is no connection, please delete this tag.
Yian
Yian le 24 Août 2015
I found that mex can only speed up C code,so m-file can not speed up

Connectez-vous pour commenter.

Réponses (2)

Jan
Jan le 18 Août 2015

0 votes

You can call Matlab from C: mexCallMATLAB. But this has a measurable overhead. So better create a pure C-Mex file for the bottleneck of the code and avoid hopping back to Matlab.

2 commentaires

Yian
Yian le 28 Août 2015
So it means that matlab code need to be translate to C code first to do mex ?
Walter Roberson
Walter Roberson le 28 Août 2015
"mex" is a command for compiling external code that might be written in C or C++ or Fortran (or any other compatible language), and arranging that compiled machine code so that it can be called as an extension from MATLAB.
"mex" does not provide the facility to compile MATLAB code into machine code.
The product that allows some portions of MATLAB to be converted into C or C++, that could then be compiled into machine code, is MATLAB Coder.
You may also have seen reference to MATLAB Compiler. It does not compile MATLAB code into machine code: instead internally it is like MATLAB without the command line, to allow single-purpose programs to be distributed without the recipients needing to own MATLAB licenses. MATLAB Compiler executables are the same speed as MATLAB, executing the same way.
If you have a section of MATLAB code that you need to speed up, the first thing you should do is profile it to figure out where the parts are that take the most time. Examine those parts first. Determine whether there might be a faster approach, such as using vectorization. Experiment.
If you identify a section that takes a long time in MATLAB and that you cannot find a way to speed up in MATLAB, then you can write C or C++ code to perform the same operations, and then have MATLAB call that C or C++ code. "mex" would be used as part of that.
If you want to have a program translate your MATLAB code into C or C++, instead of you spending your time writing that code yourself, then you need the MATLAB Coder product. It is expensive. It is also only available for the Academic or Professional / Commercial licenses. MATLAB Coder does not support all MATLAB facilities. It also takes effort to do a proper conversion even with MATLAB Coder, writing in hints to reduce ambiguity that MATLAB would normally resolve at execution time.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 24 Août 2015

0 votes

See the MATLAB Coder product.

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange

Question posée :

le 18 Août 2015

Commenté :

le 28 Août 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by