Clear Filters
Clear Filters

MATLAB coder: C + JIT or C++

4 views (last 30 days)
I have been generating some MEX functions with the sole purpose of using within the MATLAB engine at faster speed.
My questions are:
1 - Normally what is advised to tweak in the options to enforce code run speed (not build time / readability)? Is the default OK for this? I do not intend to use things that imply in loss of functionality (e.g., use integers whenever possible instead of double type). Also, the only coder command I used is coder.inline('always').
2 - Normally what is faster? C and JIT compilation or C++? It seems JIT compilation is not compatible with C++ and the coder issues a warning if tried this setting.

Accepted Answer

Richard McCormack
Richard McCormack on 12 Dec 2023
Hi Gustavo,
1) Yes, I think that in general the default settings are your best bet here. Off the top of my head, there are couple of extra settings you can try adjusting to see if they improve your performance:
2) In general I don't anticipate any performance difference between C and C++ generated code, so I would start with C and JIT.
Hope this helps, and hope you enjoy using MATLAB Coder :)

More Answers (1)

Infinite_king on 13 Dec 2023
Edited: Infinite_king on 13 Dec 2023
Hi Gustavo Lunardona,
I understand you're seeking MATLAB coder settings to enhance the generated Mex speed. While the default settings are generally effective for optimized Mex generation, you can follow these steps to potentially achieve additional speedup,
  • Set the below code generation configuration object options to true,
% create a code generation configuration object
cfg = coder.config;
% options
cfg.CacheDynamicArrayDataPointer = true;
cfg.EnableMemcpy = true;
% Note - These options will be 'true' by default, unless you change these
% settings.
  • You've already used coder.inline('always'), which can improve performance by allowing the compiler to inline certain functions. However, excessive inlining can lead to larger code size.
  • Select suitable data types. Opting for fixed-point or integer types instead of double, where applicable, can enhance performance without compromising accuracy. The effectiveness depends on the specific application.
  • Consider memory layout and access patterns for improved cache locality.
  • Finally generate code,
% generate code
codegen -config cfg -args {arg1,arg2} function_name
  • When it comes to speed, there is no significant difference between C and C++
  • Try the suggestions by 'Richard' in the previous answer.
For more information, refer the following MATLAB documentation,
Hope this is helpful.
  1 Comment
Gustavo Lunardon
Gustavo Lunardon on 15 Dec 2023
Thank you for answering my question and complementing the previous answer!

Sign in to comment.


Find more on MATLAB Coder in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!