Why is EACH function evaluation inside a parfor loop much faster then ONE evaluation outside the loop?

1 view (last 30 days)
Basically I calculate a finite difference Jacobian of a function, as shown below.
The function evaluation labelled SINGLE RUN takes about 60 seconds, and the ones labelled PARALLEL RUN take about 20 seconds.
Any clue why this might be?
Note: the "dx=0.1" does not have any influence on the speed of myfun.
myfun = @(x).... %the function of interest, which has a tic/toc inside it
[Jac] = jacob_paral(myfun,x0)
function [Jac] = jacob_paral(myfun,x0)
%SINGLE RUN:
y0 = feval(myfun,x0);
nn = length(x0);
Jac = NaN(length(y0),nn);
parfor ix = 1:nn
dx = zeros(1,nn)
dx(ix) = 0.01;
x = x0+dx;
%PARALLEL RUN:
y = feval(myfun,x);
Jac(:,ix) = (y-y0)./dx(ix)
end

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!