MEX-file efficiency with multiple file-pass function calls in .m script

If I have a script (.m-file) that calls several functions which are in separate scripts or defined at the end, I could improve its speed by in-lining my functions. If I choose to mex the main script instead I will also get a speed-up. Which is better? Further, if I inline all my function in the matlab script and then mex it, will this case be the fastest of the four cases ( .m script with file-passing functions, .m script with inlining functions, mex of file-pass script, mex of inlined script) or would it make no difference?

Réponses (1)

Jan
Jan le 21 Déc 2021
Do you use the terms "function" and "script" correctly?
Functions and scripts are stored in .m-files. Functions start with the keyword "function", scripts don't.
"If I choose to mex the main script instead I will also get a speed-up" - calling a Matlab function from inside a mex function by mexCallMATLAB needs a certain overhead, so this is expensive.
There is no general decision on what is "better" or faster. It depends on the code and on the processed data. Use the profiler and tic/toc to find the bottlenecks of the code. Then improve the bottleneck, e.g. by mexing.
Remember that "speed" can mean different things. Sometimes the pure run-time is meant, but for real applications the time until a problem is solved is more important:
t_total = t_design + t_programming + t_debugging + t_documenting + t_testing + t_runtime
Write a complete code with a clean structure (e.g. avoid scripts, global variables and keep code, data and gui strictly separated) and test it. If it works reliably, use the profiler to find out, where improvements are most efficient: Accelerating some code, which takes 2% of the processing time only, can save only up to 2% of the total run-time.
Inlining all functions might be a little bit faster now, but if a multi-threaded C-mex function for a specific job is available, you have to refactor the code and test it again. Saving some seconds of run-time is not a benefit, if it costs days to modify the code.
"Premature optimization" is one of the most frequently applied pitfalls and a "programming anti-pattern".

3 commentaires

SB
SB le 21 Déc 2021
Modifié(e) : SB le 22 Déc 2021
Thanks, Jan, for the quick response and the tips you gave. Let me be a bit more clear in what I was asking
So let's say I have a script 'test.m' which calls Acalc.m, Bcalc.mand Ccalc.m. Because I have been trying to combine several MATLAB codes (different authors) in my project, I ended up with the following, rather convoluted, code structure. Acalc.m and Bcalc.m have several functions in them, one main function and several helper functions called inside the main function, like this:
Inside Acalc.m:
function Aout= Acalc(Ain1, Ain2..) %define main function
..
out1=funct1(in1) % call helper function 1
out2=funct2(in2) %call helper function 2
% Aout is some mathematical function of out1 and out2
end %main function ends here
function O1= funct1(I1) %define helper function 1
..
end
function O2= funct1(I2) %define helper function 2
..
end
Similar for Bcalc.m. Mind you there are several helper functions not just two. Ccalc.m has only one main function.
I need to call test.m inside a loop which is very slow even after parallelizing (parfor). To gain speed I converted test.m to a function, mex-ed it using the MATLAB coder and used the test.mex function inside my loop. The resulting single mex file I get has all the information and routines from test, Acalc, Bclac and Ccalc giving me the same output as test.m and right now it is indeed significantly faster than running test.m as a function inside my loop.
Before mex-ing function test.m I profiled it and made it and Acalc, Bcalc and Ccalc as efficient as I could except removing the overheads from all the zillion function calls. I really didn’t want to change the code structure at the time and mex-ing the code after some improvement in efficieny was giving me a significant speed-up. Now I want more speed and have the time to rearrange/re-write code. So I come back to my question: I know that if I remove as many function calls from test.m and create a new function newtest.m which combines all functions inside Acalc, Bcalc, Ccalc into one or only a few functions, it will likely be faster than test.m. However, will newtest.mex be significantly faster than test.mex? How may newtest.m compare to test.mex (if this is even answerable)?
I am not familiar with the generated mex-code and can't readily figure out if the inefficiency of function calls in matlab carries over to the generated mex function. Hope this makes some sense.
@SB: It depends. If you replace mean(x) by sum(x)/numel(x), the speedup is surprising. A lean version of polyfit is 20 to 60 times faster than the built-in version.
In a mexed version, the overhead of calling local M-functions is smaller, if they are included in the mex file also. But calling external M-functions is more expensive.
There I assume, that the question, which version has the sorter run-time, cannot be answered in general.
Usually optimizing the processing speed reduces the maintainability of the code, so the programmer has to find a balance.
Thanks, Jan.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Question posée :

SB
le 21 Déc 2021

Commenté :

SB
le 23 Déc 2021

Community Treasure Hunt

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

Start Hunting!

Translated by