Improving speed of nested loops
3 views (last 30 days)
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.
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
x1=min1 + ((max1 - min1)*x1t);
x2=min2 + ((max2 - min2)*x2t);
x3=min3 + ((max3 - min3)*x3t);
x4=min4 +((max4 - min4)*x4t);
% for l=1:60
x8=min8 + ((max8 - min8)*x8t);
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?