MATLAB: Usage of structure as input of a function. Is it efficient

MATLAB: Usage of structure as input of a function. Is it efficient

functionsMATLABmatlab coderoptimizationstructures

Hello Mathworks community,
I have a general questioning about the efficiency / relevance of using structures as function inputs. I’ll try to be clear…
I’m working on a code that has around 300 variables. I find very convenient to gather / sort these variables as fields of a few (~5) structures to sort them by “type”. That clears my workspace and allow me to make shorter function calls. For example, I have a structure Model that contains fields .param1 .param2 … param40 (all different, can be scalars, small vectors or small 3D matrices).
So, when I want to make some computation, I just do :
[output] = some_function(Model, other_input) ;
Furthermore, I use in my code a lot of functions / sub-functions / sub-sub-functions… I know that this might not be the most relevant in MatLab, but I think I have a good reason to do so. First that simplifies the structure of my code since I have a lot of sequential actions, so having small divided tasks is way more easy to manage. And secondly I intend to translate the code in C using MatLab Coder to run on an embedded platform.
That scheme was fine for me, until I started working on optimization / computing time and realized that there was a big difference between the 2 solutions below:
function [output] = some_function(Model, other_input)
% [...]

% Solution 1
% (tic)

% (for i = 1:1e6)

output = some_sub_function(Model.param1, Model.param2, Model.param3, other_input) ;
% (end)

% (toc) ==> ~4 seconds
% Solution 2
P1 = Model.param1 ;
P2 = Model.param2 ;
P3 = Model.param3 ;
% (tic)
% (for i = 1:1e6)
output = some_sub_function(P1, P2, P3, other_input) ;
% (end)
% (toc) ==> ~0.4 seconds, 10 times faster !
% [...]
Since computing time is critical for my work, I start to doubt about the choices I made. The thing is that if I stop using structures, I’ll have gigantic function calls to write. And if I stop using functions, I’ll have a gigantic code to naviguate into…
I’ve struggled to find ressources online about that matter (maybe I didn’t find the right keywords), so I am curious about advices or feedback you could have ?
Thanks !

Best Answer

  • “I’ve struggled to find ressources online about that matter (maybe I didn’t find the right keywords”
    No, it’s not your fault, TMW rarely makes any comment about such thing. They thing’s it’s not programmer business to understand how things work. I just dislike the way they hide the details for programmers.
    I worked with MATLAB for about 20 years, and I did a lot of timings trying to understand how things work. Mex programming also helps since it exposes me to mxArray data structure and that gives the hints what behind MATLAB statements.
    In your case, accessing the structure fields, MATLAB store the fieldnames as char-arrays and everytime you call
    it must goes throught all the list of fielnames of the structure, doing string comparison until it matches the string ‘param1’, then it returns the corresponding mxArray (structure field). That explains why it is not fast to use it within the for loop. If you move the out
    p1 = s.param1
    the fieldname matching is done outside the loop and p1 strores the mxArray ready to be used.
    Personally I do a lot of function arguments like you, meaning few structures as input arguments (usulay one nested structure) and single output as output. Just do need to be careful with for-loop and prepare the body of for-loop to have very simple operations, and avoid calling functions, accessing structure fields, table, multiple-level indexing, etc…. and you’ll be fine. You need also to pay attention to function/statement that make memory-copies of your data, this also kill the performance of MATLAB. Using object programming is also slow, so avoid them especially if they don’t deal with large/complex data and comes with array. Most of the time that comes in the purest form as “vectorization” and the for-loop disappears entirely.
  • Add a Comment

    Your email address will not be published. Required fields are marked *