Sharing large structure with nested functions with function handles increase main function overhead?
Afficher commentaires plus anciens
Good day!
I am building a large simulation model using matlab. I have a quite complitcated structure with many sub structures and data containing model states and I have found that much of the computational power is spent on shuffeling this structure to and from functions where I edit the model states. I decided to try out some nested functions that share this complicated structure, so no copying is necessary. However, the nested functions seem to incur a massive overhead charge, using just one nested function increases the self execution time ('end' in profiler) to 60% of the main function, this is compeared to using a .m file function insted of nested.
Some relevant information:
I'm calling the function from a for loop a couple of thousand of times 4000-6000.
I'm using function handles in a cell array and calling them like so: Output = cell_array{i}(Input);
The shared structure is modified several times inside the for loop by different functions that are specified in m-files.
The nested function seems to execute faster than the .m function, but the total execution time is around 100% slower.
Is there something that can explain this increase in overhead? It would be a massive improvement not having to copy data between functions so I would very much appricieta some input!
Thanks!
3 commentaires
Ted
le 17 Nov 2023
I cannot say for sure what the cause is, but it is likely that workflows involving nested functions are less optimized than flat functions in m-files.
Since you have found that copying parts of the structure is consuming lots of processing time, I wonder if converting your structure into a handle class hierarchy would help. Passing handle objects between functions and editing their properties should be a lightweight operation.
Are you willing to share more details about the structure you're working with, and how the functions edit the state? It may help us come up with some more ideas.
Stephen23
le 17 Nov 2023
"I have a quite complitcated structure with many sub structures and data containing model states and I have found that much of the computational power is spent on shuffeling this structure to and from functions where I edit the model states."
Flatter data is often much more efficient to process than deeply nested data. It usually makes code simpler too.
"It would be a massive improvement not having to copy data between functions so I would very much appricieta some input!"
MATLAB does not copy data every time you pass something to a function. Search this forum for "copy on write" to know how it really works, as well as reading this documentation:
Edvin W
le 21 Nov 2023
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Numeric Types dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!