What is the most efficient way of calling two related functions in Matlab?

2 views (last 30 days)
Hi, I am wondering if one of the two following options are more efficient/better to use while coding
This:
a = functionA(5);
b = functionB(a);
Or This:
B = functionB(functionA(5));
Which way would be more efficient to find the same value, B?

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jul 2018
The second is faster by a small amount:
  • no entry needs to be made in the symbol table (but the entry would just be a name pointing to data that was being carried around anyhow)
  • There are checks for timer interruption every line, and the first form has two lines, so the second form has one fewer check for interruptions
  2 Comments
Walter Roberson
Walter Roberson on 28 Jul 2018
But it's always a good idea to time.
If you run this and look at the graphs, you can see that the first runs are sometimes slower for an indefinite period (blue is higher but eventually drops) and that the second round of running an identical call can sometimes be faster. But you will also see that the long term average appears to favor separate statements -- the opposite of what I said. I cannot account for this.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!