Executing parfor takes more time than executing standard for loop

조회 수: 5 (최근 30일)
Hi,
I have some big data to compute and I've read Parallel Computing Toolbox would speed up some calculations. As we have access to Parallel Computing Toolbox, I though I would give it a go. I read just a few pages of a manual and tried to do some tests. Unfortunately, the time to get through standard for-loop is significantly less than using PCT. I did the same test on 2 different computers and the results are the same. I have validated that PCT can work on each computer before I did tests. Probably I am missing something but I don't know that would be. I tried different loops: less iteration with heavier computations, much more iteration with less computation etc. but always PCT take more time to get through than standard for-loop.
So what I did was I executed command
parloop
to start Parallel Computing Toolbox, waited until the toolbox got lunched and then executed this script
clc;
N = 10*1024*1024;
M = 10;
t = 0:1/N:1-1/N;
A = zeros(N,M);
for it = 1:10
tic;
for f = 1:M
A(:,f) = sin(2*pi*f*t);
end
tt = toc;
fprintf('%30s: %.2f secs\n', 'Non-parallel computing', tt);
tic
parfor f = 1:M
A(:,f) = sin(2*pi*f*t);
end
tt = toc;
fprintf('%30s: %.2f secs.\n', 'Parallel computing', tt);
end
Did I misunderstand something?
Thanks

채택된 답변

Image Analyst
Image Analyst 2016년 4월 19일
You have very very small loops. So few iterations (10) that the overhead of setting up processes on different CPUs is probably more than you gain from having it be parallel. You should see improvements as your iterations get bigger, like in the millions.
  댓글 수: 1
Zbigniew Koziel
Zbigniew Koziel 2016년 4월 19일
I did not realised overhead would cause that. I set the loops for 1 million iterations and parallel is 4 times faster now. Thanks.

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

추가 답변 (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