MATLAB: PCT: Parfor vs For with one core

pct parfor for

We are writing an application in which some parts are meant to be in parallel. Nevertheless, we want to tune the app so that it uses the optimal number of cores (even if the number of cores = 1)
My question is: what happens with the speed of execution for these two blocs
parfor i = 1:X
executeY
end
for i = 1:X
executeY
end
when only one core is being used, keeping everything else constant? Is the parfor slower because it checks if a matlabpool is open? Is the speed of execution the same for both?

Best Answer

Try it!
But I can let you know that parfor will be slower due to overhead;
function test
[t1, t2] = deal(0);
a = 1;
b = 2;
x = 3;
%Sum their times over 1000 function calls:
for ii = 1:1000
tic
for jj = 1:1000
y = a*x+b;
end
t1 = t1+toc;
tic
parfor kk = 1:1000
yp = a*x+b;
end
t2 = t2+toc;
end
%Display results:
fprintf(1,'\nFOR: %fs\nPARFOR: %fs\n',t1,t2);
fprintf(2,'\nSlowdown: %f\n\n', t2./t1);
I'm seeing a slowdown in the high 80x
Related Question