How to speed up my code
古いコメントを表示
I haven't been able to vectorize the code below. Below,
lent=600, lens=950, K=400.
I have values for B, d1, d2, ExpSum2, d1star, and d2star.
The size of d1,d2,d1star,d2star are all 950x600x400.
The size of B is 1350x600.
The size of ExpSum2 is 400.
I have values for bs, and length(bs)=1550.
I have posted two sets of for loops below. They should do the same thing, but the first one ran faster for me.
I have also vectorized the multiplication B.*d2 and pulled it outside the for loop, but I couldn't vectorize
B(s+k,tt).*d1(s,tt,k).
I still had to run the for loop, and the run time increased. Right now it takes the program between 2 hours and 2hours and 20 minutes to run.
Is there a way to significantly speed this up? I was hoping I could vectorize this, but if not I may try a mex file. Thanks for your time.
for tt=1:lent
for k=1:K
for s=1:lens
if bs(s)>.1
J(s,tt,k)=0;
else
J(s,tt,k)=.99^k*(B(s+k,tt)*d1(s,tt,k)-B(s,tt)*d2(s,tt,k)*ExpSum2(k));
end
if J(s,tt,k)<0
J(s,tt,k)=0;
end
if bs(s)>.1
Je(s,tt,k)=0;
else
Je(s,tt,k)=.99^k*(B(s+k,tt)*d1star(s,tt,k)-B(s,tt)*d2star(s,tt,k)*ExpSum2(k));
end
if Je(s,tt,k)<0
Je(s,tt,k)=0;
end
end
end
end
for tt=1:lent
for k=1:K
for s=1:lens
if bs(s)<=.1
J(s,tt,k)=max(0,.99^k*(B(s+k,tt)*d1(s,tt,k)-B(s,tt)*d2(s,tt,k)*ExpSum2(k)));
Je(s,tt,k)=max(0,.99^k*(B(s+k,tt)*d1star(s,tt,k)-B(s,tt)*d2star(s,tt,k)*ExpSum2(k)));
end
end
end
end
7 件のコメント
Adam
2018 年 6 月 13 日
J and JE don't appear to be being pre-sized unless you left that code off. Obviously if vectorisation is possible then pre-sizing will likely become irrelevant though.
Brian Hogle
2018 年 6 月 13 日
dpb
2018 年 6 月 13 日
If you preallocated, then J, JE will be zero everywhere they're not set to anything else, already so looks like you could rewrite the code like
if bs(s)>.1
J(s,tt,k)=0;
else
J(s,tt,k)=.99^k*(B(s+k,tt)*d1(s,tt,k)-B(s,tt)*d2(s,tt,k)*ExpSum2(k));
end
to
if bs(s)<=0.1
J(s,tt,k)=.99^k*(B(s+k,tt)*d1(s,tt,k)-B(s,tt)*d2(s,tt,k)*ExpSum2(k));
end
that won't save a ton, but storing a zero on top of an existing one doesn't do anything useful... :)
After that, it appears that you could simply wait until done and then write
J(J<0)=0;
after the nested loop and save the if inside the loops as well; it doesn't appear that subsequent J are dependent on earlier values so leaving the <0 entry while computing doesn't appear to matter.
I've got other commitment; will try to take a look at the other Q?? re: vectorizing later...
Stephen23
2018 年 6 月 13 日
"I wasn't sure how much of the code to post"
Post all of your code by uploading it as an attachment: click the paperclip button to do this.
Brian Hogle
2018 年 6 月 13 日
I'd suggest kutting the sizes down and also attach a .mat file containing sufficient data to run a test case if expecting anybody to do more than just air-code/read what have.
Also, looks like a lot more code there than just this section; have you profiled to determine where the actual bottleneck is for certain?
Brian Hogle
2018 年 6 月 13 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!