Improving speed of nested loops

3 views (last 30 days)
sita
sita on 21 Jun 2013
hi,
I am trying to run nested loops as mentioned in below code.I didn't even see the result even once.It is taking such a long time.Please help me how can i make it faster and how can i estimate time for the same.So that i can wait for those many hours.
Thanks, Sita
clear; clc;
tic p1 = haltonset(1); p2 = haltonset(2); p3 = haltonset(3) ; p4 = haltonset(2) ; p5 = haltonset(2) ;
counti=0; countj=0; countk=0; countl=0; countm=0;
n=1; s=1;% number of times to run
min1=2; max1=3;
min2=2;
max2=3;
min3=2;
max3=3;
min4=2;
max4=3;
min5=2;
max5=3;
min6=2;
max6=3;
min7=2;
max7=3;
min8=2;
max8=3;
min9=2;
max9=3;
min10=2;
max10=3;
for t=1:s
for i=1:10
x1t=p1(i,1);
x1=min1 + ((max1 - min1)*x1t);
counti=counti+1;
for j=1:40
x2t=p2(j,1);
x2=min2 + ((max2 - min2)*x2t);
x3t=p2(j,2);
x3=min3 + ((max3 - min3)*x3t);
countj=countj+1;
for k=1:60
x4t=p3(k,1);
x4=min4 +((max4 - min4)*x4t);
x5t=p3(k,2);
x5=min5 +((max5-min5)*x5t);
x6t=p3(k,3);
x6=min6 +((max6-min6)*x6t);
countk=countk+1;
% for l=1:60
x7t=p4(l,1);
x7=min7+((max7-min7)*x7t);
x8t=p4(l,2);
x8=min8 + ((max8 - min8)*x8t);
countl=countl+1;
for m=1:100
x9t=p5(m,1);
x9=min9+((max9-min9)*x9t);
x10t=p5(m,2);
x10=min10+((max10-min10)*x10t);
countm=countm+1 ;
yal(countm)= fc10(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10);
minvals=min(yal);
end
end
end
end
end
end
toc

Accepted Answer

Roger Stafford
Roger Stafford on 21 Jun 2013
You are executing the line containing 'fc10' 144 million times, so you must expect your code to take a long time. If it is an array it would be extremely large so I am guessing it is a function call. If so, that means all the overhead of making the function call is repeated that many times, not to mention whatever computation is performed each time.
What is your final goal in the code? Do you want the entire 'val' array or only its minimum value?
There is a marked inefficiency in the way you generate the x1, x2, ..., x10 combinations. For example, there are only 100 possible values of x10, but each one is recomputed from p5 over a million times. This computation should be done only once for each different value. In the beginning before entering the nested for loops you should create a vector x10:
x10 = min10+(max10-min10)*p5(1:100,2);
which does the computation only once for each x10 value. The same applies to all the other x values from 1 to 9. Then each of the five nested for loops would merely extract the appropriate x values directly from the x arrays without going through all that computation.
On the line "minvals=min(yal)" you are doing this minimum computation the same 144 million times and each time the 'min' function has a longer array to search for a minimum. This is extremely inefficient. You should wait until all 'val' value have been computed before finding their minimum, which would mean placing this line after the nested for-loops are finished.
You are computing several counts: counti, countj, etc., but you seem to make use of only one of them, namely countm. Why is that?
Probably more important than any of the above is to reconsider your need for so many different combinations of variables to be received by 'fc10'. Isn't there a more efficient way of achieving the same objective?
  1 Comment
sita
sita on 24 Jun 2013
Edited: sita on 24 Jun 2013
Thanks for your detailed answer.It was very useful.I tried changing my code as mentioned.Is there anything mode can i modify to improve speed.
clear; clc;
tic p1 = haltonset(1); %x1 %grnrtates 10 dimentional halton sequence p2 = haltonset(2); %x2,x3 p3 = haltonset(3) ; %x4,x5,x6; p4 = haltonset(2) ; %x7,x8 p5 = haltonset(2) ; %x9,x10
countm=0;
n=1; s=1;% number of times to run
min1=2;
max1=3;
min2=2;
max2=3;
min3=2;
max3=3;
min4=2;
max4=3;
min5=2;
max5=3;
min6=2;
max6=3;
min7=2;
max7=3;
min8=2;
max8=3;
min9=2;
max9=3;
min10=2;
max10=3;
for t=1:s
for i=2:2
x1t=p1(((i-1)*10^6: i*10^6),1);
x1=min1 + ((max1 - min1)*x1t);
for j=2:5
x2t=p2(((j-1)*10^6: j*10^6),1);
x2=min2 + ((max2 - min2)*x2t);
x3t=p2(((j-1)*10^6: j*10^6),2);
x3=min3 + ((max3 - min3)*x3t);
for k=2:7
x4t=p3(((k-1)*10^6:k*10^6),1);
x4=min4 +((max4 - min4)*x4t);
x5t=p3(((k-1)*10^6:k*10^6),2);
x5=min5 +((max5-min5)*x5t);
x6t=p3(((k-1)*10^6:k*10^6),3);
x6=min6 +((max6-min6)*x6t);
for l=2:7
x7t=p4(((l-1)*10^6:l*10^6),1);
x7=min7+((max7-min7)*x7t);
x8t=p4(((l-1)*10^6:l*10^6),2);
x8=min8 + ((max8 - min8)*x8t);
for m=2:11
x9t=p5(((m-1)*10^6:m*10^6),1);
x9=min9+((max9-min9)*x9t);
x10t=p5(((m-1)*10^6:m*10^6),2);
x10=min10+((max10-min10)*x10t);
countm=countm+1 ;
yal= fc10(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10);
minvals( countm)=min(yal);
whose minvals
end
end
end
end
end
end
toc

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!

Translated by