Does passing large structures, such as a GUI handles structure, to a function increase execution time?

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

"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."
Sounds quite reasonable. That is the recommended practice.
"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?"
I would say there is no problem passing the handles structure around. MATLAB does not copy the data just because you passed it to some other function (unless it is changed in the function, a feature known as copy on write) so there is unlikely to be any performance penalty for passing that data. Read this to know more:
In general copy on write means that normally you should just write your code without worrying about what is happening in the memory. Only if there is a clear problem should you try to deal with it (and currently you have not stated any particular problem, just a question asking if you are doing things in a good way.... which you are).
" 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..."
Perfect. The time saved by writing concise, easy to read, easy to understand, easy to debug code likely hugely outweighs the time you would spend trying to write some complex ways to cheat MATLAB's memory management. Do NOT fall into the trap of premature optimization!

More Answers (1)

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

Products

Release

R2016a

Tags

Asked:

on 31 May 2018

Edited:

on 31 May 2018

Community Treasure Hunt

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

Start Hunting!