Function call takes a long time

Hi all, I am writing a simulation code in MATLAB R2011b. I need it to run for at least 100,000 time steps, preferably more.
Here's the problem I am facing. I have a function call within my parent function. When I run the profiler, it shows that about 23.8% of the total time is spent CALLING the function. For one of my simulation models, the function gets called 715,903 times and the function call takes 174.432 seconds according to the profiler. The called function itself takes 195 seconds to run. My parent function takes 537 seconds.
This is what my function call looks like -
[C] = Decision(k,timeStep,MixedBuffer,c);
Is there any way I can reduce the 174.432 seconds (~3 minutes) which are taken by the function call? I hope I have been descriptive enough, but feel free to ask for more info.
Best, Sumant
Here's the relevant code -
function [C] = Decision(k,timeStep,MixedBuffer,c) %Returns serial number of chosen part.
global PartTable
global ActiveParts
global DemandArrivals
if(c==1) %For machine 1
I = zeros(1,5);
ChoiceMatrix = zeros(1,size(PartTable,2)); vertcat(ChoiceMatrix,PartTable(ActiveParts(serial,1),:));
ChoiceMatrix = vertcat(ChoiceMatrix,DemandArrivals);
ChoiceMatrix(1,:) = [];
if(isempty(ChoiceMatrix)==1)
C(1,1) = timeStep;
C(1,3) = 1;
else
[~,row] = min(ChoiceMatrix(:,1));
C = ChoiceMatrix(row,:);
C(1,3) = C(1,3)+1;
end
C;
else
I = zeros(1,5);
ChoiceMatrix = zeros(1,size(PartTable,2));
[serial,~] = find(ActiveParts(:,3)==k-1);
ChoiceMatrix = vertcat(ChoiceMatrix,PartTable(ActiveParts(serial,1),:));
k;
ChoiceMatrix(1,:) = [];
ActiveParts;
[~,row] = min(ChoiceMatrix(:,1));
C = ChoiceMatrix(row,:);
end
end

 Risposta accettata

Jan
Jan il 16 Dic 2011
There are some dummy lines in your code, which do not assign anything:
vertcat(ChoiceMatrix,PartTable(ActiveParts(serial,1),:));
C;
k;
ActiveParts;
Such dummy lines waste time.
In "if (isempty(ChoiceMatrix)==1)", the "==1" is not needed. The "I = zeros(1,5)" is not used.
Using GLOBALS requires time. It might be possible, that this time appears in the calling line, when values are updated or copied.

5 Commenti

Thanks Jan!
I use some of those dummy lines for testing and seeing what values I get for variables like C and k etc. I'll take them out in the final version of my code.
The vertcat thing was a copy+paste error. Sorry!
Your GLOBALS comment was very helpful, thanks! It did reduce the function CALL time by about 47 seconds. The GLOBALs were taking up approximately 47 seconds too! This is very strange to me! I wouldn't have thought the function call time to be affected by the GLOBALs. So I suppose cleaning up the rest of the function which is called should also reduce the function calling time. I will try out.
Best,
Sumant
I did try it out, and it helped! The function call now takes about 80 seconds as compared to the earlier 174 seconds. I do have some more time-intensive 'find' operations in the function. I'll try and see if I can save more time there.
I appreciate your help Jan. Thanks a lot!
So, instead of using global variable, you pass in those variables as input arguments? Is it your conclusion here that passing in variables takes less function call time than using global variables?
That is what I did. And it certainly seems more efficient, and so I would conclude that passing those variables as input arguments rather than declaring them GLOBAL saves time. I also did what you suggested yesterday, namely cleaning up my code for redundant lines/assignments. And that too saved time on the function call. Thank you!
Good to know, it makes sense. Thank you!

Accedi per commentare.

Più risposte (4)

Walter Roberson
Walter Roberson il 15 Dic 2011

0 voti

Do you modify any of those parameters within the function? If so there could be copying overhead.
Sumant Raykar
Sumant Raykar il 15 Dic 2011

0 voti

Hi Walter,
Nope, none of the parameters within the function are modified during one call of the function. From one call to the other, some parameters change values.
Best, Sumant

2 Commenti

Jan
Jan il 15 Dic 2011
Please post the relevant part of the code.
Remove any "clear all" statement you have!

Accedi per commentare.

Pay attention to the warning message in M-editor. It gives you many hints on improvement. Some of them are minor. Others may have a big impact.
Two lines of I=zeros(1,5) are not needed because "I" is never used.
Input argument MixedBuffer is not used.
ChoiceMatrix = zeros(1,size(PartTable,2));
vertcat(ChoiceMatrix,PartTable(ActiveParts(serial,1),:));
ChoiceMatrix = vertcat(ChoiceMatrix,DemandArrivals);
ChoiceMatrix(1,:) = [];
In the above code, the first vertcat() has no returns. It's a waste of time. Why do you add an all-zeor row to the first row and then delete it. It's the same as ChoiceMatrix=DemandArrivals;
isempty(ChoiceMatrix)==1 is the same as isempty(ChoiceMatrix)
[serial,~] = find(ActiveParts(:,3)==k-1), no need to use the second return argument.
Another vertical concatenation and then removing.
The variable ChoiceMatrix seems unnecessary. Just use the global variable directly.
Don't use C and c as different variables. They are different but easily confused.
Sumant Raykar
Sumant Raykar il 15 Dic 2011

0 voti

Thanks Fangjun! I appreciate it.
But do you think the points you mentioned are causing the function CALL to take so much time?
Best, Sumant

3 Commenti

It should not but it's hard to say definitely without having the complete data and code to run the profile. I would try to clean up the code as much as possible and then run the profile. You can always drill down to see which part takes the time, right?
True.
I was searching for this all over, and I read something about 'garbage disposal' and stuff. Ex: http://stackoverflow.com/questions/4268113/matlab-takes-a-long-time-after-last-line-of-a-function
Does this seem relevant? Currently every function call takes on the average 0.000243653 seconds, which isn't high but since I call it over 700k times, it amounts to a lot.
If that's something you want to save, you can put the code of your function inside your main function to avoid the function call.

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by