What is faster in MATLAB/Simulink? The code written as .m-File or a code written in C++ and translated with MEX to MATLAB?

답변 (1개)

Jan
Jan 2018년 7월 2일
편집: Jan 2018년 7월 2일

0 개 추천

It depends.
A compiled C++ file is not translated to Matlab. You can call it from Matlab like an M-function, but it is a compiled dynamic library.
For a simple a*b, when a and b are large vectors, Matlab calls an optimized BLAS library, which is multithreaded. Of course you can do this from C++ also, but if you use a naive loop, you will be slower - maybe up to a factor of the number of cores in your CPU.
How do you define "faster"? If it is the time to solve a problem, you have to consider:
t_total = t_design + t_programming + t_documentation + t_debugging + t_runtime
Do not measure the runtime only. Only for real-time applications and if the runtime is some days or weeks, the other contributions can be neglected.

댓글 수: 5

Guillaume
Guillaume 2018년 7월 2일
How do you define "faster"?
Indeed!
  • faster to write?
  • faster to debug?
  • faster to maintain?
  • faster to run?
Michael Simonovski
Michael Simonovski 2018년 7월 2일
편집: Michael Simonovski 2018년 7월 2일
My ask was for a real-time application, which is simulated in MATLAB/Simulink, so i wanted to ask what is faster to run MATLAB - Code or MEX Code?
Jan
Jan 2018년 7월 2일
편집: Jan 2018년 7월 2일
And my question is: it depends. In many cases C code runs faster, but not in all. And it is possible to write inefficient C code, which runs slower than a comparable M code.
Michael Simonovski
Michael Simonovski 2018년 7월 2일
Are there some examples, rules and so on how to write a fast MEX code?
Guillaume
Guillaume 2018년 7월 2일
i wanted to ask what is faster to run MATLAB - Code or MEX Code?
The first question should be: which are you proficient in? Can you write C code as well as matlab code? The simple fact that you're asking the question would indicate that you're probably not that experienced with either, in which case, the learning curve will most likely be easier in matlab.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Call C++ from MATLAB에 대해 자세히 알아보기

태그

질문:

2018년 7월 2일

댓글:

2018년 7월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by