Is the C code generated by Matlab Coder Faster than my Matlab Code?

21 views (last 30 days)
Benson Gou
Benson Gou on 9 Mar 2021
Edited: Jan on 24 May 2021
Dear All,
I am thinking to buy a Matlab Coder to convert mt Matlab code into C code. My purpose is to largely reduce the CPU time of my code.
To convert my Matlab code into a C code, I need to modify my Matlab Code accordingly, which is a big investment of my time.
I have a question for those who have the experience in converting Matlab code into C code: is the C code generated by Matlab Coder really faster than Matlab code? It is worthy to buy Matlab Coder?
Thanks a lot.
Benson

Accepted Answer

Jan
Jan on 9 Mar 2021
Edited: Jan on 24 May 2021
A good question. I do not think, that there is a general answer. It will depened on your code. If e.g. the file transfer is the limiting factor, a faster SSD is more important than the Coder.
If runtime matters, optimizing the Matlab code is important. But it might be hard to impoissible to convert the highly optimized Matlab code to C. So a fair comparison between the Coder and pure Matlab code requires to optimize (and test) two different code versions.
If the optimization (and testing) takes a week and you save 2 minutes of run time, this is interestingly from a scientific point of view only.
There are many examples in this forum for optimizing code by exploiting the underlying maths. Accelerating the calculations cannot compete with omitting them. An example:
x = 1:10;
y = 11:20;
[X, Y] = meshgrid(x, y);
% Slow: 100 expensive EXP calls:
Z1 = exp(-X * sin(0.1) - Y * cos(0.1));
% Fast: 20 EXP calls:
Z2 = exp(-x * sin(0.1)) .* exp(-y.' * cos(0.1));
The standard procedure of optimization is:
  1. Debug the code and test it exhaustively in the initial version. Improving failing code is a complete waste of time. You need a trustwothry version to compare the results after each step of optimization.
  2. Use the profiler to identify the bottlenecks.
  3. Analyse the maths and adjust the model to reduce computations.
  4. Check if a (partial) vectorization improves the speed.
  5. Compare with the bottleneck improved by the Coder.
  6. Parallelize the code, if many cores of the CPU are in idle mode during the processing.
  7. Buy a faster computer (or if step 6 is fine: 10 faster computers).
  4 Comments
Walter Roberson
Walter Roberson on 24 May 2021
The time for the fprintf() itself will be pretty much the same; MATLAB calls into the C fprintf() function to do the work for Coder (this does mean that some of the fprintf() features supported in MATLAB are not supported in Coder.)
The time marshalling values to print might be slightly faster with Coder generated code, as it would not be necessary to go through a run-time lookup of symbol and pull out the address and size and type.
The fprintf() itself needs to interact with the operating system or file system, which takes time; and the interaction with the device is likely to take the longest time.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!