What causes successive runs of nested function to be slower?

3 views (last 30 days)
I'm running into a problem that is outside of my experience with MATLAB. I have a function that has repeated calls to a nested function. Each run of the nested function takes longer than the first, in an alternating pattern of approximately 15 seconds longer, then 100 seconds longer (listed below).
I cannot post the full function at this time, but here are some relevant parts.
  • Parent function defines all input variables for nested function.
  • runFactors (array) is passed to nested function as input argument, all other variables used are from within parent function
  • Output is a pair of arrays
  • No variables are cleared during the function run (only overwritten)
  • Sub function does simple addition/multiplication of multiple configurations from multiple input arrays (e.g. A(1) + B(1) + C(1), A(2) + B(1) + C(1), A(2) + B(2) + C(1), etc.) and reports the maximum.
  • Size of arrays does not change between function calls.
  • The same input data (arrays) is used for each run of the nested function.
The section from the parent function calling the nested function:
%Running stress calculations
waitbar(0,h,'Calculating Stresses...');
time0 = toc
[stressFull,mpFull] = calcStressesBend(runFactors(1,:));
time1 = toc - time0
[stressLessPres,~] = calcStressesBend(runFactors(2,:));
time2 = toc - time1
[stressLessUllage,~] = calcStressesBend(runFactors(3,:));
time3 = toc - time2
[stressLessAero,~] = calcStressesBend(runFactors(4,:));
time4 = toc - time3
[stressLessTherm,~] = calcStressesBend(runFactors(5,:));
time5 = toc - time4
[stressLessED,~] = calcStressesBend(runFactors(6,:));
time6 = toc - time5
[stressIntOnly,~] = calcStressesBend(runFactors(7,:));
time7 = toc - time6
[stressThermOnly,~] = calcStressesBend(runFactors(8,:));
time8 = toc - time7
[stressEDOnly,~] = calcStressesBend(runFactors(9,:));
time9 = toc - time8
[stressAeroOnly,~] = calcStressesBend(runFactors(10,:));
time10 = toc - time9
[stressUllOnly,~] = calcStressesBend(runFactors(11,:));
time11 = toc - time10
[stressPresOnly,~] = calcStressesBend(runFactors(12,:));
time12 = toc - time11
Runtime results:
  • time1 = 119.30 s
  • time2 = 134.89 s (15.6 s longer)
  • time3 = 238.45 s (103.6 s longer)
  • time4 = 252.17 s (16.7 s longer)
  • time5 = 356.10 s (103.9 s longer)
  • time6 = 370.89 s (14.8 s longer)
  • time7 = 474.17 s (103.3 s longer)
  • time8 = 488.28 s (14.1 s longer)
  • time9 = 591.66 s (103.4 s longer)
  • time10 = 605.60 s (13.9 s longer)
  • time11 = 709.23 s (103.6 s longer)
  • time12 = 722.91 s (13.7 s longer)
Is there something causing each call to create a copy of the variables in the parent function? Is there some simple change to prevent this (persistent, etc)?
Thanks for your help, Joel

Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!