Does passing large structures, such as a GUI handles structure, to a function increase execution time?
Show older comments
In essence I often find when building GUI's that large amounts of my code are replicated and therefore I try to write functions which can be used multiple times.
An example of this are when I have multiple sliders with corresponding edit boxes where changing the value in one updates the other. I found the concisest way of writing the function was to pass the entire GUI handles structure to the function with a reference to which slider / edit pair I wanted to update.
However does passing a large number of variables (within the handles structure) that are essentially unused dramatically increase the execution time of the function / is it considered bad practice?
Appreciate this is a very general broad question with lots of different cases and therefore probably difficult to answer so thanks in advance :)
Accepted Answer
More Answers (1)
Ameer Hamza
on 31 May 2018
Edited: Ameer Hamza
on 31 May 2018
1 vote
In MATLAB handle classes follow the behavior of referenced variable in C/C++. When you pass a variable of handle class to a function. MATLAB does not create a copy of the handles. In fact, it will just pass the address of the original memory location. Therefore you don't need to worry about the overhead of passing handle variables. Even for simple class variables, MATLAB follows copy-on-write strategy and will not create a copy unless you explicitly change the value of the variable inside the function. Refer here for a similar discussion.
Categories
Find more on Debugging and Improving Code 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!