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
댓글 수: 0
채택된 답변
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.
추가 답변 (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!