Filter löschen
Filter löschen

Reduce processing time for the simulation

4 Ansichten (letzte 30 Tage)
Santhosh Chandrasekar
Santhosh Chandrasekar am 22 Feb. 2018
Hi, i have the below-mentioned code and it is taking 25 minutes for 1000000 iterations. Can someone suggest the way to optimize the total processing time? I think the for loop in line 23 is taking the major processing time. Is there any way to optimize it.
V= 0.002;
C=0.002;
I= eye(221);
s = string({'CR';'E';'R';'S';'SR'});
[x,y] = ndgrid(1:20,[2,5]);
str = s([2;3;2;4;y(:)]) + ([101;1;106;1;x(:)] + (0:4));
str = cellstr([s(1),str(:)']);
d = {tril(ones(44,4),-1),diag(ones(20,1),-24)};
d{1}(4:end,3) = 0;
d{2} = d{2}(:,1:end-4);
dd = repmat({[d{:}]},1,5);
dd = [zeros(1,221);[ones(220,1),blkdiag(dd{:})]];
out = [{nan},str;str(:), num2cell(dd) ];
A= cell2mat(out(2:end,2:end));
result=zeros(221,4,1000);
for i=1:10
A1= 0 + (1-0).*rand(221,1);
X11= inv(I-(A))*A1;
P= zeros(221,1);
P(:)= X11(:)/sum(X11);
D1= 0 + (1-0).*rand(221,1);
Utility=0;
for j=1:221
Utility= (((1-P(j))*(D1(j)/(D1(j)+A1(j)))* V)-C) +Utility;
end
result(:,1,i)=A1;
result(:,2,i)=D1;
result(:,3,i)=P;
result(1,4,i)=Utility;
end
[Utility_max,index_max]=max(result(1,4,:));
A1_max=result(:,1,index_max);
D1_max=result(:,2,index_max);
P_max=result(:,3,index_max);

Akzeptierte Antwort

John D'Errico
John D'Errico am 25 Feb. 2018
Bearbeitet: John D'Errico am 25 Feb. 2018
Sorry, but this is just godawful code. Hey, don't hate the messenger. :) I know that it takes time to improve coding skills. But there is no way I'll do more than a quick read through it. Trying to guess how to improve that code hurts my eyes.
Time for you to learn how to use the profile tools in MATLAB. But there are obvious things I can see. For example, you have a loop that starts out as:
for i=1:10
A1= 0 + (1-0).*rand(221,1);
X11= inv(I-(A))*A1;
Inside the loop, A is FIXED. I is FIXED. Both are of size 221x221. So I-A is FIXED. Yet you compute inv(I-A) each of 10 times through the loop. Even if A changes outside of that loop (and I don't see where it does) you are computing that matrix inverse 10 times, when it needs be computed only once before the loop starts. (In fact, you would arguably be better off if you never computed the inverse at all. But that is another thing to discuss.)
On my (admittedly oldish CPU)
IA = rand(221);
timeit(@() inv(IA))
ans =
0.0018644
So 1 million iterations?
1e6*ans
ans =
1864.4
So it appears you are spending roughly 1800 seconds just computing a matrix inverse repeatedly, when you could have saved 90% of that time, just by computing the inverse outside of the loop!
And since you claim this whole process requires roughly 1/2 hour to run, I'd suggest that a large fraction might just be in that matrix inverse.
Again, only you can know this for sure, since my computer is moderately old. While it was moderately souped up about 7 years ago, it is slow by current standards. But even so, that will be a significant factor.
Basically, look for places where you can remove some computation from the loop. Rather than doing some computation 10 times, do it once.
You might also be able to gain from the decomposition utility, newly introduced into MATLAB.
But most importantly, learn to profile your code. That is perhaps the most important thing you can do on a problem like this.
  1 Kommentar
Santhosh Chandrasekar
Santhosh Chandrasekar am 25 Feb. 2018
Thank you very much for your inputs and I never gonna hate you :). I sincerely appreciate you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by