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

5 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Walter Roberson
Walter Roberson il 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.

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by