Effacer les filtres
Effacer les filtres

How to make this function run faster

4 vues (au cours des 30 derniers jours)
Derek Cooper
Derek Cooper le 4 Fév 2021
Modifié(e) : Derek Cooper le 22 Avr 2021
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

Réponse acceptée

dpb
dpb le 4 Fév 2021
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.
  1 commentaire
Derek Cooper
Derek Cooper le 5 Fév 2021
Thank you very much

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur GNSS Positioning dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by