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

1 vue (au cours des 30 derniers jours)
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Juil 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 commentaires
Ak K
Ak K le 28 Juil 2018
Thanks for your descriptive and prompt response!
Walter Roberson
Walter Roberson le 28 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by