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

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

采纳的回答

Walter Roberson
Walter Roberson 2018-7-28
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 个评论
Walter Roberson
Walter Roberson 2018-7-28
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by