필터 지우기
필터 지우기

Why is parfor-loop is much slower than for-loop in this case?

조회 수: 11 (최근 30일)
Simon
Simon 2023년 3월 25일
댓글: Simon 2023년 3월 28일
I just started to explore the pros and cons of parfor-loop. In the simpy testing codes below, parfor-loop is much slower than for-loop. I don't understand why. Any feedbacks are appreciated. (My project is to process a large number of tables and collect the parsing data into a huge long table.)
for loop
(In my 8-core Mac, the elapsed time is in the order of 0.004 seconds.)
C = cell(2,1);
tic
for i = 1:2
A = rand(1,10);
T = array2table(A);
C{i} = T;
end
toc
Elapsed time is 0.033399 seconds.
parfor-loop
(In my 8-core Mac, the elapsed time is in the order of 0.03 seconds.)
D = cell(2,1);
tic
parfor i = 1:2
A = rand(1,10);
T = array2table(A);
D{i} = T;
end
Starting parallel pool (parpool) using the 'Processes' profile ...
toc
Elapsed time is 18.526561 seconds.

채택된 답변

Image Analyst
Image Analyst 2023년 3월 25일
There is some setup time required to set up the different CPUs. If you have only 2 iterations and are using such tiny variables like you are, it's usually/mostly not worth it. Try with much larger variables and millions of loop iterations and see if it's better.
  댓글 수: 2
Simon
Simon 2023년 3월 25일
Thanks! So I did as you suggested, increasing the rand( ) to rand(20,20) and iteration to 20,000. The for-loop has elapsed time of 5.6 seconds, which is still faster than parfor-loop whose elapsed time is 6.7 seconds.
For-loop looks fast enough and has less overhead cost. It should be suitable to do the job for me.
Simon
Simon 2023년 3월 28일
I tried again using parfor on my original tall table. In this real case, parfor-loop showed its power. All the 8 cores in the machine cranked up. The whole job is finiished less than 600 seconds. Amazing. Thanks for your explanation, which helps me to do it with my real data set.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by