Is there a significant advantage in hand-writing MEX code vs using the automatic MATLAB Coder project?

Hi,
I have a moderately complex matlab function which is acting as a bottleneck in my project.
I could rewrite it in C++ and turn it into a mex file, which would take me probably a week or two, or I could just use the MATLAB Coder products "codegen" function to create the mex file automatically which would be much easier.
So my question is, will there be any speed advantage in me converting it by hand, or is it basically pointless for me to try to outsmart the codegen process?
Thanks,
Henry

2 个评论

Is this code not improved by using gpuArray? You should definitely look into that first.
Have you profiled your function to identify where specifically it's spending most of its time?
If you can simplify the problem down to a small self-contained section of code and you can describe what types/sizes/etc. the variables used in that section are, could you post that code section and that variable information? With that information the experts in Answers may be able to offer some suggestions for how to modify the code to make it more efficient.

请先登录,再进行评论。

回答(1 个)

Are you good at coding? How good? If so, then you can probably improve on automatically generated code.
However, it is also often true that just spending the same amount of time in improving your code will give as much of a speed bump, just by writing better MATLAB code.
Much depends on what your code is doing. If you spend a great deal of time performaing operations that are already highly optimized in MATLAB, already done in fairly tight compiled code already, then you don't rate to gain much. In fact, your hand written code may slow things down.
I'd spend time in using tools like the Parallel Computing toolbox and your GPU, if an option. Make the most of the resources you have available. So are you currently effectively using the cores in your CPU? If not, and you have the fan capacity to keep them cool, then you can probably gain a lot more than you will by writing your own code.

类别

在 帮助中心 和 File Exchange 中查找有关 MATLAB Coder 的更多信息

产品

版本

R2020b

提问:

2020-11-2

Community Treasure Hunt

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

Start Hunting!

Translated by