rodms
rodms

Reputation: 391

Simple example of MATLAB's parfor

I am trying to use parfor to speed up some algorithms, but it is just slowing them down. I found a number of resources on the internet talking about this issue, but I couldn't find an answer to my problem.

I have the following code:

n = 2^14;
v = randn(n, 1);

tic;
for i = 1:n
    v(i) = v(i) + 1;
toc

Elapsed time is 0.000200 seconds.

matlabpool

tic;
parfor i = 1:n
     v(i) = v(i) + 1;
end
toc

matlabpool close

Elapsed time is 0.069568 seconds.

Why is this happening? How can I use parfor to speed-up this simple MATLAB example?

Thank you.

Upvotes: 1

Views: 244

Answers (1)

damienfrancois
damienfrancois

Reputation: 59072

Your test example is too small/simple so the overhead of creating the additional processes for the matlabpool is large with respect to the time needed to perform the computations. Try a computation that lasts at least for several dozen seconds.

Upvotes: 3

Related Questions