MATLAB Coder: C Version Much Slower Than Mex Version
이전 댓글 표시
I used MATLAB Coder to turn my MATLAB code into C code and expected a great decrease in run time, but instead found that my C code runs much slower than both the MATLAB code and the mex code. Over 5 tests, I get the following:
- Tthe MATLAB code took 77.48 seconds on average.
- The mex version running in MATLAB took 46.27 seconds on average.
- The generated C code took 262.35 seconds on average.
All three versions of the code produces the exact same output, so I am confident that each version is working properly. I am running the MATLAB and mex versions in R2023a and the C version in Debian 11 (bullseye) in a docker container. The cmake command I am running is
cmake -DCMAKE_BUILD_TYPE=Release ..
and my understanding is that this enables the C compiler optimisations, but I'm unsure if there are further optimisations I can enable. Before specifying the build type as release, my C code ran about 90 seconds slower still (~350s), which is horrendous compared to the MATLAB/mex version.
These are the settings I am generating my code with:

The full settings I used are attached in config.mat but they are almost all just the default settings.
My understanding is that MATLAB does some computations in parallel automatically, while the generated C code doesn't, so I tried turning on automatic parallelisation in the generated code. This only made the code run slower, however. I'm not sure how the automatic paralellisation works, but when I manually tried to parallelise parts of my code, it also did not run faster, so I did not really expect this to improve anything.
I'm just really unsure why the C version runs so much slower than even the mex version, which I expected to be similar or worse than the C version. Are there settings I can tweak in Coder? Are there further optimisations I can enable on the Linux side of things? If this is normal behaviour, what is the explanation for the discrepency between C and mex?
댓글 수: 8
dpb
2024년 9월 29일
Builtin MEX functions are handcoded and have been optimized/worked on for years; the Coder is machine-generated code that is portable and generic; it's not terribly surprising it isn't as efficient.
The optimizations object basically turns on specific compiler flags, but those do nothing to markedly affect the code generator itself that converts the MATLAB m-code files to C.
What, if anything, you could do to cause the Coder to generate better, faster code would pretty-much be dependent upon whether you could find ways to improve the m-file code itself to produce a better translation -- but there are no real rules for there other than the generic guidelines in the documentation about optimization that I'm sure you'll have already looked at.
While sometimes one can make things markedly faster by converting m-files to mex; it isn't always the case and when it is possible it generally is the result of knowing how to write efficient C code that bypasses higher-level MATLAB constructs, not just that C is faster than MATLAB. That used to be true, but no longer with all the advances in the JIT compiler; if one is doing mostly numeric operations and the code is vectorized, the base MATLAB m-file code may well run essentially just as fast as optimized C code--it does, after all, boil down to eventually calling the highly-optimized BLAS or LAPACK libraries for basic operations and unless you have such code and help the Coder along in calling the standalone libraries, it will generate base transportable C code instead.
Nyx
2024년 9월 30일
Naman Saraf
2024년 10월 4일
Hi,
Is it possible for you to share your MATLAB code to understand the performance issues.
Thank you,
Nyx
2024년 10월 4일
dpb
2024년 10월 4일
Note that LAPACK/BLAS won't be magic bullets unless your code/problem is such that it boils down to linear algebraic operations that match up to what routines are in the libraries.
Probably the next place to go would be to examine the performance of the C code itself; "pre-optimizing" before knowing just where the bottlenecks are is likely to be nonproductive. See <Profiiling C Code>
Nyx
2024년 10월 6일
Nyx
2024년 11월 4일
Christine Tobler
2025년 1월 7일
You may want to try a different version of the BLAS (and possibly also of LAPACK). You can find out the versions that are used by MATLAB on your installation by calling
>> version -lapack
>> version -blas
There are many build options that could affect the performance (e.g., is threading enabled?).
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Execution Speed에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!