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

2 次查看(过去 30 天)
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

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by