Why parfor loop so efficient?

조회 수: 1 (최근 30일)
C.Hu
C.Hu 2016년 5월 30일
답변: Walter Roberson 2016년 5월 30일
Hi I tested two script in the following
%1. Parall_try
parfor i = 1:2000
for j = 1:10000
b(i,j) = i*j+i/j;
end
end
and
%2. Serial_try
for i = 1:2000
for j = 1:10000
a(i,j) = i*j+i/j;
end
end
I set two workers totally. The profile report shows 32s for running serial_try, and about 2s for parallel_try. I noticed the parallel running does not output any i/j index to workspace, which may explain the speed up of parallel running. Is this really true? It motivates me to find a more efficient way to run my script. In script, it output every variable to workspace, which can be very time-consuming. Can I calculate them without output them to workspace? It may sounds absurd because I have not figured out why parfor loop can be that efficient.
  댓글 수: 2
jgg
jgg 2016년 5월 30일
Do you pre-allocate those arrays? That could be the difference.
C.Hu
C.Hu 2016년 5월 30일
No, I clear the workspace everytime before running the script.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 5월 30일
parfor effectively pre-allocates the arrays before use whereas your non-parfor solution does not. That makes a big difference in efficiency.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by