Executing parfor takes more time than executing standard for loop

2 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 19 Apr 2016
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 Comment
Zbigniew Koziel
Zbigniew Koziel on 19 Apr 2016
I did not realised overhead would cause that. I set the loops for 1 million iterations and parallel is 4 times faster now. Thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!