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 :)
采纳的回答
更多回答(1 个)
Ameer Hamza
2018-5-31
编辑:Ameer Hamza
2018-5-31
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Debugging and Improving Code 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!