I am new in MATLAB and I just try to execute a parallel small program, but the problem is that the parallel execution take more time than serial ?
close all
clear all
clc
a= rand(1e6,1);
b= rand(1e6,1);
c= zeros(size(a));
d= ones(size(c));
e= zeros(size(d));
tic
tstart=tic;
for i=1:length(a)
c(i)=a(i)+b(i);
d(i)=c(i)+b(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
e(i)=d(i)+c(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
c(i)=a(i)+b(i);
d(i)=c(i)+b(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
e(i)=d(i)+c(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
c(i)=a(i)+b(i);
d(i)=c(i)+b(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
e(i)=d(i)+c(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
end
t_normal_for=toc(tstart)
tstart=tic;
parfor i=1:length(a)
c(i)=a(i)+b(i);
d(i)=c(i)+b(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
e(i)=d(i)+c(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
c(i)=a(i)+b(i);
d(i)=c(i)+b(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
e(i)=d(i)+c(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
c(i)=a(i)+b(i);
d(i)=c(i)+b(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
e(i)=d(i)+c(i)*a(i)+b(i)/a(i)+b(i)*a(i)+b(i);
end
t_parfor=toc(tstart)
***************************************
t_normal_for =
0.3860
t_parfor =
2.8403
Can anyone help! In fact I have 4 workers in my computer and the version of MATLAB is R2014a. Another question please, can I send the same function to each worker in my computer?
Thank you in advance Ammar