Does the use of name-value arguments in functions slow down the speed of my code?

5 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2023-2-23
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 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by