What is the difference between MATLAB code files, pCode and MEX files?

6 次查看(过去 30 天)
I hear about all of these, but do not know how they differ.

采纳的回答

Doug Hull
Doug Hull 2011-1-18
  • M-files are plain ASCII text that is interpreted at run time. Actually it is parsed once and "just-in-time" compiled, but this is transparent to the user. Use M-files for most of your MATLAB development, and for platform independence and maintainability.
  • Pcode is a preparsed and encoded version of the M-file. Since it is preparsed, it saves on the load time of the function. This is most likely not an issue except for very large M-files, since most are parsed only once anyway. Pcode also lets you hide the source code from others. Careful, there is no way to convert Pcode back to the M-file source. Pcode is platform independent.
  • MEX files are native C or C++ files that are dynamically linked directly into the MATLAB application at runtime. They must be compiled for each hardware architecture on which they are to be run. MEX files have the potential to crash the MATLAB application, but rather large speed gains are possible, depending on the algorithm.
[From the MATLABFAQ of Ancient Times]
  2 个评论
James Tursa
James Tursa 2011-2-3
MEX files can also be Fortran source :)
Another difference is that arguments to MEX routines are passed by reference whereas arguments to m-files are passed a shared-data-copy. One exception is that cell or structure field references are resolved before the MEX routine is called, so in those cases the MEX routine gets passed a shared-data-copy of the cell or structure field. Another difference is that MEX routines can be MATLAB version dependent, so a MEX routine compiled under one MATLAB version may not run under another MATLAB version, even on the same machine.
James Tursa
James Tursa 2012-4-27
Also, Pcode is MATLAB version dependent. Pcode generated under one MATLAB version may not run under a different MATLAB version.

请先登录,再进行评论。

更多回答(2 个)

Alexei
Alexei 2011-2-12
I actually want to add a question ))
I know you can turn .m code to C files using the MCC and then turn that to a MEX dll
Question is, can you turn .p code to C using MCC and then turn to a MEX dll?
  1 个评论
Walter Roberson
Walter Roberson 2011-2-12
mcc cannot turn .m files to C using any version for a number of years now. mcc produces data files that contain threaded interpreted code, not C code. mcc has no problem including .p files in producing the final result.

请先登录,再进行评论。


Rui
Rui 2012-4-27
is there any difference in performance on using the MCC to build a standalone application or matlab compiled dll to be called within a C/C++ application?
  1 个评论
Walter Roberson
Walter Roberson 2012-4-27
No, other than perhaps differences in the start-up time (and those differences could depend upon whether a graphics window was being created.)
However, in the other question you were asking about this, you were talking about converting code to C or C++. Converting code to C or C++ is handled by MATLAB Coder, not MATLAB Compiler, and the result would run at the speed of C (or C++), not at the speed of MATLAB. The trade offs is that MATLAB Coder is expensive and there are quite a number of things that cannot be compiled with it.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by