Executing parfor takes more time than executing standard for loop
2 views (last 30 days)
Show older comments
Zbigniew Koziel
on 19 Apr 2016
Commented: Zbigniew Koziel
on 19 Apr 2016
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 Comments
Accepted Answer
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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!