필터 지우기
필터 지우기

How to make this function run faster

조회 수: 4 (최근 30일)
Derek Cooper
Derek Cooper 2021년 2월 4일
편집: Derek Cooper 2021년 4월 22일
Hello,
I have this function inside a scheme which takes 70% of the total time of running the script. The function decomposes a 4th order tensor such that
for i = 1:2
for j = 1:2
D_UL = [C(1,1,1,1) C(1,1,2,2) C(1,1,1,2);
C(2,2,1,1) C(2,2,2,2) C(2,2,1,2);
C(1,2,1,1) C(1,2,2,2) C(1,2,1,2)];
end
end

채택된 답변

dpb
dpb 2021년 2월 4일
For starters, try...
...
IJ=inv(J);
for i = 1:2
for j = 1:2
for k = 1:2
for l = 1:2
AA = IJ*F(i,1)*F(j,i)*F(k,1)*[D1111*F(l,1) + D1112*F(l,2)];
BB = IJ*F(i,1)*F(j,i)*F(k,2)*[D1121*F(l,1) + D1122*F(l,2)];
CC = IJ*F(i,1)*F(k,1)*F(j,2)*[D1211*F(l,1) + D1212*F(l,2)];
DD = IJ*F(i,1)*F(j,2)*F(k,2)*[D1221*F(l,1) + D1222*F(l,2)];
EE = IJ*F(i,2)*F(j,1)*F(k,1)*[D2111*F(l,1) + D2112*F(l,2)];
FF = IJ*F(i,2)*F(j,1)*F(k,2)*[D2121*F(l,1) + D2122*F(l,2)];
GG = IJ*F(i,2)*F(j,2)*F(k,1)*[D2211*F(l,1) + D2212*F(l,2)];
HH = IJ*F(i,2)*F(j,2)*F(k,2)*[D2221*F(l,1) + D2222*F(l,2)];
C(i,j,k,l)=C(i,j,k,l)+ AA+BB+CC+DD+EE+FF+GG+HH;
end
end
end
end
...
The next step is to move the partial products that are invariant in the inner loops out to the outer loop...for example, the term IJ*F(i,1) is invariant inside the loops over j,k,l and so could be computed in the i loop and made a temporary there. Then, that term times F(j,I) is invariant inside loops over k,l so could be done at the j loop level and that temporary carried through. Etc., etc., etc., ...
Just moving the inv(J) outside all the loops should be noticeable, however, factoring out the common terms should also be of some benefit.
You'll want to check carefully I didn't get eyes crossed, but I think I have the correct factors in each.
Also, of course, while it's not shown here, the array C should definitely be preallocted.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 GNSS Positioning에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by