Functions vs scripts: speed

37 views (last 30 days)
Alessandro D
Alessandro D on 26 Jun 2018
Commented: Steven Lord on 27 Jun 2018
Dear all,
The structure of my code is the following:
** VERSION A**************
output = fun1(inputs)
Piece of code written directly
in the function
************************** **** VERSION B **********
output = fun1(inputs)
% The same piece of code is written
% in a script, that is called by the function
Both versions give exactly the same results, but version A is much faster. I think version B is better in terms of code readability (I prefer to break the code in smaller chunks), so I would like to know if someone has an idea why version B is slower
  1 Comment
Stephen on 27 Jun 2018
"why version B is slower"
Excluding any possible effects from JIT optimization, it is quite reasonable that B would be slower. Compare:
  1. search MATLAB path for function file fun1
  2. run function code
  1. search MATLAB path for function file fun1
  2. run function code
  3. search MATLAB path for script file script1
  4. run script code using magic run...
Note that scripts should be avoided. Scripts are fun for playing around with, but code that needs to be efficient, testable, and repeatable will use functions (or classes).

Sign in to comment.

Accepted Answer

OCDER on 27 Jun 2018
Glad you're thinking about coding practice and optimization.
Version B is worse in terms of time, debugging, and coding practice. Version B "poofs" in variables into your fun1 local workspace, making it difficult to figure out what went wrong. You also have to use the run function, which is in itself a function that uses the evalin function which is in itself a complex and slow function. Instead of "script1", make it a function by itself.
function output = fun1(inputs)
a = script1(inputs);
output = a+1;
The issue with version B:
Say you have 2 scripts:
script1.m contains:
a = 2;
script2.m contains:
for a = 1:10
b = a;
function output = fun1(inputs)
output = a; %Which a are you referring to? WHERE did a come from? a = 10. Ooops, forgot script2 uses "a".
Steven Lord
Steven Lord on 27 Jun 2018
If script1 only needs to be called from within fun1, consider making it a nested function inside fun1. If you do that, it can "share" variables with fun1.
If it needs to be callable from within fun1 and from other functions, one approach to avoid the tedium (and potential error of forgetting one or adding an extra one) is to pack the inputs into a struct array (or an object) and pass the struct between the functions.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 27 Jun 2018
Option B is a REALLY bad programming style. It is slow, as you found out. It prevents MATLAB from optimizing your code for efficiency.
In general, learn to avoid scripts. Put the heavy lifting work into functions, although sometimes a mainline script to combine everything together is not a bad thing. What you have here in B is the worst of worlds.
As you learn to write functions, making your code modular, you will be able to reuse code for a variety of problems. That gains on programming time, for your NEXT project. It helps you to debug your code, because you work on one small function at a time. Get each part correct, then worry about the next part.
Is option B better for readability? Why would it be so? Sorry, but not true. Learn to write and use functions. Your code will be better for it, especially as you start to write MODULAR code.
  1 Comment
Jan on 27 Jun 2018
+1. Exactly. Prefer functions. They are nicer, faster, more reliably and easier to debug and to maintain.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!