简体   繁体   中英

Parallel code is taking longer time than sequential code using Parallel computing toolbox in MATLAB. Why?

I am working with MATLAB. I am just new with parallel computing toolbox in MATLAB. I have core i3 processor, MATLAB R2011a, 2 GB of RAM, 320 Hard disk.

To calculate speed up, I just wrote following code and found that parallel code is taking longer time than a sequential code.

1st code is taking 0.039763 seconds

2nd code is taking 0.379056 seconds .


1st code:

tic
MM = magic(5);
MN = magic(5);
ML = magic(5);
MP = magic(5);
MK = magic(5);
MM
MN
ML
MP
MK
toc

2nd Code:

matlabpool open local 4

tic
spmd  % Uses all 3 workers
    MM = magic(5); % MM is a variable on each lab
end
MM{1}
MM{2}
MM{3}
MM{4}
toc
matlabpool close

I want to learn parallel computing toolbox.

As mentioned by Dan in the comments, the problem is clearly too small for parallelization to be beneficial. Increasing for example the size of the magic matrices you create from 5 to 5000, already shows a clear improvement. That is, with the larger size the overhead of parallelization becomes (almost) negligible compared to the computation time for one matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM