- 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.
- Use the profiler to identify the bottlenecks.
- Analyse the maths and adjust the model to reduce computations.
- Check if a (partial) vectorization improves the speed.
- Compare with the bottleneck improved by the Coder.
- Parallelize the code, if many cores of the CPU are in idle mode during the processing.
- Buy a faster computer (or if step 6 is fine: 10 faster computers).

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

31 views (last 30 days)

Show older comments

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

##### 0 Comments

### Accepted Answer

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:

##### 4 Comments

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.

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!