Is it better to have one function or multiple instances of one code?
Show older comments
Hello everyone. I was wondering if it is computationally cheaper to have MATLAB run multiple instances of the same code rather than running a custom function repeatedly. For example, I could make a simple set of conditional statements and a couple of really fast matrix operations and place that where needed (copy-and-paste and maybe slight adjustment). The code probably takes a few milliseconds which causes me believe that a function would make it slow. In my case, I want to run the same set of code three or four times. Would the overhead of a function make it significantly slower?
Also, can someone give me a hyperlink to specific documentation that explains when it's best to start thinking about making a function rather than extending the current script?
I don't really know how to search for this answer on the internet. Thanks!
1 Comment
"when it's best to start thinking about making a function rather than extending the current script?"
Right now! Scripts are less reliable than functions, what with all of those variables hanging around interfering with each other, and slower too: "Use functions instead of scripts. Functions are generally faster."
"The code probably takes a few milliseconds which causes me believe that a function would make it slow"
MATLAB does all kinds of intelligent runtime optimizations behind the scenes, and so any optimization should be based on tests and good practice. By writing code based on what you presume might be the case you cut yourself off from actually first writing good code and then optimizing if required. Good code in this case means avoiding copy-and-paste, which is prone to errors, makes code maintenance a horror, and makes code less understandable (which leads to more bugs). You would be much better off writing clearly defined functions and testing them thoroughly. Worry about optimization later, once you know where the actual bottlenecks are.
Accepted Answer
More Answers (1)
Walter Roberson
on 15 Jul 2017
1 vote
It appears that anonymous functions have lots of overhead. See test code attached.
It appears that on my system, each layer of pure function call adds about .05 * 10^(-6) overhead (that is, about 5E-8 seconds), but each level of anonymous function adds 2 to 3 * 10^(-6) (that is, about 2.5E-6 seconds).
3 Comments
Philip Borghesani
on 19 Jul 2017
Edited: Philip Borghesani
on 19 Jul 2017
One thing to remember is that anonymous functions do quite a bit of work. I added nested function tests with function handles to Walter's example and they run faster than the anonymous functions.
Tyler Warner
on 20 Jul 2017
Walter Roberson
on 20 Jul 2017
I extended Philip's extension of my tests. I estimate from the results that a single level of anonymous function adds about 0.47 seconds per million.
I also experimented using timeit() instead of tic/toc, but the individual function times were too small for timeout to measure with any accuracy.
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!