Is the C code generated by Matlab Coder Faster than my Matlab Code?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
2 votes
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
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:
- 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).
4 Comments
Benson Gou
on 9 Mar 2021
Hi, Jan,
Thanks a lot for your suggestions. I will study your suggestions and try to improve my Matlab code. Actually I spent a lot of time to do this already. I improved my algorthms and try to use vector or matrix calculations instead of using for iterations. I will keep doing it, anyway.
As to the faster computer, do you think Dell XPS is much faster than Dell Inspiron with the same core devices?
Thanks a lot again.
Benson
All currently available computers have almost the same speed (forget Celerons and Pentiums). This is a coarse claim, I know. For multithreaded code the number of cores matter a little bit. But the underlying idea is: Today spending the double money gives a speed gain of just 10% or 20%. Waiting for 2 years will give you 50% speed for the same money. Seeing this over a period of 20 years, the quality of the computers are less significant than the date of the production.
I've read a paper yesterday, in which a professor explained, that only one of his students solved a homework by a brute force attack: Find x * (1/x) ~= 1 for 1<=x<=2. The approach:
c = 2^-52;
for k = 0:2^52; x = 1 + k * c; if x * (1/x) ~= 1, disp(k); end; end
The professor wrote, that the code runs for 24 hours until the first match was found.
On my computer or in Matlab online this needs less than a half second. So what is the difference: The paper was written 1996.
This let me estimate, that a Dell XPS or Inspirion do not have an substantial difference without even taking a look in the the specifications. A CPU of Intel starting with an "i" or a Ryzen will do the job.
When I was young, it was too hard to decide between a 386 or 486 CPU and I've lost a complete year with reading concerning publications, benchmarks and magazines. Finally I've bought an Apple with a PowerPC cpu. Today my strategy for optimizing my work is:
- Buying used hardware with an age of 3 years for some 100 Euro. Only business PCs are reliable enough, because searching compatible drives or BIOS updates wastes a lot of time.
- Replace the disks and invest in stable backup system. A disk crash can cost a month, while saving a month of processing time is nearly impossible.
- Debug each function with an exhaustive unit test, which checks clearly the processing of valid and invalid inputs. You can loose weeks with finding a deeply nested bug if e.g. MathWorks decides to change the behaviour of strncmp('', '', 2) another time.
- The documentation of code is very important, because it supports re-using it for other projects. It saves a year of programming time, if you can use or expand a well tested toolbox.
- Now the optimization of the bottlenecks can start - see my former asnwer.
As soon, as my code is ready, I can use a much larger computer with 512 nodes a 4 CPUs and Teras of memory. I will never have the money to buy such a monster, but it is cheap to rent it.
The Coder can be useful even if it sacves some percent processing time only, if real time is needed. For one of my projects a patient is measured and the examiner waits for an assessment of the trial. Witing for more than 5 seconds causes stress, physicall and mentally. The examiner looses concentration, because he does not get the feeling, that his actions cause a direct reaction of the computer. So It was extremely important to squeeze my code until it takes less than 5 seconds for a result.
I have solved this by writing C-MEX functions. They are hardcoded pure C without any comfort of Matlab. I cannot imagine that the Code can produce comparable speed, but writing an testing C code is a dry work. A perfect C code has to consider the number of cores, the sizes of the different CPU caches and the latency of the RAM. But as soon as a code is too much optimized for a specific hardware, it looses its power, when new computers are invented.
For some jobs I start Matlab 6.5 in a virtual machine, because e.g. the creation of diagrams is much faster. Producing an animation with 200 3D objects with many faces takes 20% of the processing time compared to Matlab 2018b. But of course the code is debugged in R2018b, because the Matlab 6.5 editor does not know warnings, code completion, etc.
Sorry, you see that "optimzing code with MATLAB Coder" opens a wide field. If this is worth ot not is hard to decide. Many other aspects are important for efficiency also.
Benson Gou
on 23 May 2021
Thanks a lot for your excellent explanation.
I want to reduce a general question into a more sepecific question about the speed. If my code only contains many fprintf, do you think Coder could help me to reduce the CPU time?
Thanks a lot again.
Benson
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)
Categories
Find more on MATLAB Coder in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)