Does the use of name-value arguments in functions slow down the speed of my code?
5 views (last 30 days)
Show older comments
Michael Loibl
on 23 Feb 2023
Answered: Walter Roberson
on 23 Feb 2023
The use of name-value arguments in functions allows a much more flexible use of functions. The simple alternative for me would be to write an additional function in order to have two quite similar functions without name-value arguments. Then, I would of course get a lot of duplicates of code if I would do it for my whole program.
However, I was wondering whether it would affect the speed of my program when I change a function to name-value arguments which is called multiple 1000 times?
0 Comments
Accepted Answer
Walter Roberson
on 23 Feb 2023
Yes, of course argument processing takes execution time. MATLAB does not somehow cache the results of the argument processing code from execution to execution: it runs it every iteration. Over many iterations that adds up.
However, you need ask yourself whether the cost of those CPU cycles is more than the cost of you developing and maintaining two similar but different functions.
0 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown 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!