Function call takes a long time
조회 수: 12 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Jan
2011년 12월 16일
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
추가 답변 (4개)
Walter Roberson
2011년 12월 15일
Do you modify any of those parameters within the function? If so there could be copying overhead.
댓글 수: 0
Fangjun Jiang
2011년 12월 15일
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.
댓글 수: 0
Sumant Raykar
2011년 12월 15일
댓글 수: 3
Fangjun Jiang
2011년 12월 16일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!