필터 지우기
필터 지우기

How to reduce execution time in nested for loops and struc. arrays

조회 수: 1 (최근 30일)
I have the following code, it is part of a much larger one. The total execution time for the whole code is around 50 sec. I am trying to reduce this figure to few secs. So the first thing is I started to look for for loops to reduce.
The following part of the code is taking 33 sec. (more than half the total time) and I am only creating strucs. arrays
NOC=100;
NOBS=2;
NORB_PER_BS=5;
NOU=200 ;
for k=1:NOU
U1(k).SINR_MAX=0;
U1(k).SINR_F_AVG=0;
U1(k).v=[];
U1(k).w=[];
U1(k).SINR_IND={};
U1(k).ASSIGNED=0;
U1(k).SINR_MAX_CANDIDATES=[];
for n=1:NORB_PER_BS
for b=1:NOBS
for v=1:NOU
U(k,n,b).Q_LIST=[];
U(k,n,b).Q_LIST1=[];
U(k,n,b).Q_LIST_MEMBERS=[];
U(k,n,b).Q_MIN_MEMBERS=[];
U(k,n,b).Q_MIN=[];
SINR(k,n,b)=0;
end
end
end
end
  댓글 수: 3
Guillaume
Guillaume 2019년 3월 1일
편집: Guillaume 2019년 3월 1일
In addition to Walter's comment, is the structure array preinitialised? Or is it resized at each step of the loops (a major cause of slow-down)?
Does your real code just assign the same value for each element? In that case, why use a loop when you could just repmat a constant scalar structure?
Oh, and just notice that NOC is never used.
Mohammed Hadi
Mohammed Hadi 2019년 3월 1일
@walter
Thanks for pointing that out. I kept editing the code and forgot to remove this part.
-------------------------------------------------------
@Gulliaume
The struc. array is not pre-initialized. I am using these loops to initialize it and make sure all are zeros.
The size of
U(k,n,b).Q_LIST=[];
U(k,n,b).Q_LIST1=[];
U(k,n,b).Q_LIST_MEMBERS=[];
U(k,n,b).Q_MIN_MEMBERS=[];
U(k,n,b).Q_MIN=[];
changes in the steps folllowing this part
Sorry for the NOC, yes you are corrent it is not used here but used in the following steps.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2019년 3월 1일
Your current code is equivalent to:
U = struct('SINR_MAX', num2cell(zeros(1, NOU)), 'SINR_F_AVG', 0, 'v', [], 'w', [], 'SINR_IND', {{[]}}, 'ASSIGNED', 0, 'SINR_MAX_CANDIDATES', []);
U1 = struct('Q_LIST', cell(NOU, NORB_PER_BS, NOBS), 'Q_LIST1',[], 'Q_LIST_MEMBERS', [], 'Q_MIN_MEMBERS', [], 'Q_MIN', []);
SINR = zeros(NOU, NORB_PER_BS, NOBS);
  댓글 수: 5
Guillaume
Guillaume 2019년 3월 4일
That really depends on what is going on in the loops. Sometimes, there's no way to gain any speed other than completely changing the algorithm.
Walter Roberson
Walter Roberson 2019년 3월 4일
And sometimes it is several centuries of research to find a better algorithm. Or to prove that no better algorithm exists (only implementation details.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by