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

1 Ansicht (letzte 30 Tage)
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?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentare
Ak K
Ak K am 28 Jul. 2018
Thanks for your descriptive and prompt response!
Walter Roberson
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by