How to speed up this calculation and remove loop(s)?

1 回表示 (過去 30 日間)
Daulton_Benesuave
Daulton_Benesuave 2022 年 9 月 29 日
コメント済み: Daulton_Benesuave 2022 年 9 月 30 日
Hello,
I have a table oFD that 10450x27 in size. I am trying to optimize this double loop to speed up the processing. For whatever reason, I'm drawing a blank how to optimize and am sure if can be computed more efficiently.
sPs = [3500:5000]';
CGE = zeros(size(sPs,1),1);
for row = 1:size(oFD,1)
for m = 1:size(sPs,1)
CG = my_function(sPs(m),oFD.PS(row),oFD.dTE(row),oFD.CIV(row));
CG2(row,m) = oFD.COI(row)*sPs(m)^2*CG;
end
end
Would someone please be able to help?
Thank you in advance!
  3 件のコメント
Image Analyst
Image Analyst 2022 年 9 月 29 日
What is size(oFD,1) and size(sPs,1)? Unless they're tens of millions, your bottleneck may not be the loop iteration itself but something inside the loop. If the total number of iterations is like a few hundred thousand, you're only talking about microseconds for the looping overhead.
Daulton_Benesuave
Daulton_Benesuave 2022 年 9 月 30 日
Sorry for the late reply, had to attend to same family matters yesterday afternoon.
size(oFD,1) = 10500x27
size(sPs,1) = 1797x1

サインインしてコメントする。

採用された回答

Jan
Jan 2022 年 9 月 29 日
There is a very small potential for optimizing in the posted code:
for row = 1:size(oFD,1)
c1 = oFD.PS(row);
c2 = oFD.dTE(row);
c3 = oFD.CIV(row);
c4 = oFD.COI(row);
for m = 1:size(sPs,1)
CG = my_function(sPs(m), c1, c2, c3);
CG2(row,m) = c4 * sPs(m)^2 * CG;
end
end
I assume, the main time is spent in my_function. The profiler would reveal this.
If you post the code of my_function, further improvements are possible.
  5 件のコメント
Cel Kulasekaran
Cel Kulasekaran 2022 年 9 月 29 日
Don't think it matters, Daulton's custom function is blsgamma which is already vectorized...
Jan
Jan 2022 年 9 月 29 日
I do not have the Financial Toolbox. So this is a dumb guess only:
for row = 1:size(oFD,1)
c1 = oFD.PS(row);
c2 = oFD.dTE(row);
c3 = oFD.CIV(row);
c4 = oFD.COI(row);
CG = my_function(sPs, c1, c2, c3);
CG2(row, :) = c4 * sPs.^2 .* CG;
end
So can you call my_function with a vector as 1st input?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by