Why parfor is slower than for when accessing 3-D array?
Show older comments
I want to make my for loop run faster in a program that i use a 3-D array and by experimenting i came to the conclusion that when I access a 3-D array with parfor is slower than doing it in for, whereas when I run the same code but using a 2-D array the results are the opposite.
A small example is the following.
%Storing data on 3-D array with parfor
tic
parfor (i=1:100)
for (j=2:2134)
for(x=1:1000)
k(i,j,x)=100;
end
end
end
toc
Elapsed time is 319.463914 seconds.
%storing data on 3-D array with for
tic
for (i=1:100)
for (j=2:2134)
for(x=1:1000)
k(i,j,x)=100;
end
end
end
toc
Elapsed time is 25.601181 seconds.
%Storing Data in 2-D array with parfor
tic
parfor (i=1:1000)
for (j=2:2134)
for(x=1:1000)
k(i,j)=100;
end
end
end
toc
Elapsed time is 0.774042 seconds.
%Storing Data in 2-D array with for
tic
for (i=1:1000)
for (j=2:2134)
for(x=1:1000)
k(i,j)=100;
end
end
end
toc
Elapsed time is 3.060670 seconds.
I am running in with 4 workers when in parallel.
Why is this happening when I am using 3-D arrays and how can i solve it?
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!