How I can speed up My code that contain of 8 FOR nested loops ?
1 次查看(过去 30 天)
显示 更早的评论
Hi every one , I have a code that contain of 7 nested FOR loops, each loop from 1:30 . My idea of this code is calculate many of a special functions for each value of these loops and many of files need to calling, for example :
A1= 1:30
A2= 1:30
A3=1:30
A4=1:30
A5=1:30
A6=1:30
A7=1:30
for b1=1:length(A1)
for b2= 1: length(A2)
for b3= 1:length(A3)
for b4=1:length(A4)
for b5= 1: length (A5)
for b6=1:length(A6)
for b7=1:length(A7)
N(b1,b2,b3,b4,b5,b6,b7)=M(b1,b2,b3,b4,5b,b6,b7)*T(b1,b2,b3,b4,5b,b6,b7)
end
end
end
end
end
end
end
where M and T are have a unique value for each iteration , for example M(1,1,1,1,1,1,1)=5 , M(1,2,1,1,1,1,1)=8 and so on , when I run my code its tack long time and then restart my computer without any result can you help me to speed up this code ? thank you in advance .
7 个评论
Adam Danz
2018-8-8
You haven't answered the question as to whether 'A' is a function or a 7D array. If it's an array and this example really represents what you want to do you might be able to avoid loops and replace them with a vector method which may (or may not) speed things up.
Nevertheless, given the little I know about your problem, it seems that another solution may be to process your data in chunks. For example, you could save data to a file every time A=3 loop is complete and then free up the memory in matlab so it's available for the next series of loops.
OCDER
2018-8-8
Even without knowing the class of M and T, it takes a while. 30^7 is 21,870,000,000 iterations. That's ~45s to just go through the for loop.
tic
for j = 1:30^7
end
toc %45s
Any math inside is just going to make the things slower. You may need to use parfor to divide up the compute time, or vectorize a segment of the loop to prevent memory error.
采纳的回答
David Goodmanson
2018-8-10
编辑:David Goodmanson
2018-8-10
Hi A,
I am assuming that M,T and N are 7d arrays of the same size, n x n x n x n x n x n x n for some n. For n = 30 that's 30^7 = 2.1870e+10 elements per array. Not much to say about the memory requirements, they are what they are.
As far as speed, though, in place of all the for loops you can write
X = M(:).*T(:);
NN = reshape(X,n,n,n,n,n,n,n);
On my pc the for loops start to take significant time around n = 12. It's about 8 seconds for the for loop version, compared to .2 sec for the code just above, and the ratio will get more pronounced as n increases.
----------p.s. on for loops:
You have defined A1 = 1:n (1:30 in your case), so rather than write
for b1 = 1:length(A1) etc.
you can just write
for b1 = A1 etc.
Also it makes sense to preallocate N as
N = zeros(n,n,n,n,n,n,n)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!