MATLAB: Parfor is slower than for

parallel computingParallel Computing Toolboxparfor

Dear Matlab community, I am trying to implement parfor function and have 5000 iterations. What kind of settings and preferences should I select? looking at different resources I did such commands:
parpool(8)
Result:
Connected: true
NumWorkers: 8
Cluster: local
AttachedFiles: {}
IdleTimeout: 30 minutes (30 minutes remaining)
SpmdEnabled: true
Then I run a function:
clc;
clear;
modulation = 4;
iterations = 5000;
tic
parfor i=1:iterations
if(modulation == 4)
result(1,i) = modulation*i;
elseif(modulation == 16)
result(1,i) = 2*modulation*i;
end
end
time_1 = toc;
tic
for i=1:1:iterations
if(modulation == 4)
result(1,i) = modulation*i;
elseif(modulation == 16)
result(1,i) = 2*modulation*i;
end
end
time_2 = toc;
fprintf('time Parallel = %.9f;\n', time_1);
fprintf('time Serial = %.9f;', time_2);
fprintf('\n');
Result time is as following: time Parallel = 0.150103298; time Serial = 0.000062877;
How to improve parallel run time?
Thank you

Best Answer

"I don't see a reason why parfor should be slower than for."
I don't see a reason why parfor should be faster than for. The only time it is worth using parfor is when the time saved running the operations inside the loop is greater than that of the extra time for the parfor overhead. This is computing, and nothing comes for free (something that beginners seem to forget when they expect infinitely fast computers with infinite memory, infinite-precision numerics, and overhead operations that take no time at all).
Running a parfor is a much more complicated thing than running a simple for loop because the data needs to be partitioned and the results grouped again afterwards, so it is entirely possible that a parfor might be slower. This is clearly explained in the documentation: "There is overhead in calling parfor instead of for. If function evaluations are fast, this overhead could become appreciable. In particular, solving a problem in parallel can be slower than solving the problem serially."
With your problem the actual task you perform in the loop is so fast that the parfor overhead will take considerably more time than the operation itself.
Related Question