为什么parfor远低于当开销可以忽略不计?
1视图(30天)
显示旧的评论
我有一个代码如下所示。它与常规的成本660秒
然而,随着parfor,成本超过半个小时。关键是开销可以忽略不计。
= = = = = = = = = = = = = = = = = =
清晰的所有;关闭所有;clc;抽搐
Nx = 100;纽约= 100;
plist = 0.05:0.05:0.95;
linked_list = 0(1、长度(plist));
num_sample = 20000;
matlabpool开3
parforsss = 1:长度(plist)
瑞士
p = plist (sss);
numlink = 0;
为s10 = 1: num_sample
Nx模式=(兰特(纽约)< p);
Nx pattern2 = 0(纽约);
新= 0 (10000);
new2 = 0 (10000);
num_new = 0;
num_new2 = 0;
国旗= 0;
发现= 0;
s = 0;
而(发现= = 0)& & (s < Nx)
s = s + 1;
如果(模式(1,s) = = 1) & & (pattern2 (1, s) = = 0);
国旗=国旗+ 1;
pattern2 (s) =国旗;
num_new = 1;
新(1,1)= s;
新(2,1)= 1;
num_new2 = 0;
而num_new > 0
num_new2 = 0;
为s1 = 1: num_new
x =新(s1);
y =新(s1);
如果(x > 1) & &(模式(y, x - 1) = = 1) & & (pattern2 (y, x - 1) ~ =国旗)
num_new2 = num_new2 + 1;
num_new2 new2 (1) = x - 1;
num_new2 new2 (2) = y;
pattern2 (y、x - 1) =国旗;
结束
如果(x < Nx) & &(模式(y、x + 1) = = 1) & & (pattern2 (y, x + 1) ~ =国旗)
num_new2 = num_new2 + 1;
num_new2 new2 (1) = x + 1;
num_new2 new2 (2) = y;
pattern2 (y、x + 1) =国旗;
结束
如果(y > 1) & &(模式(y-1 x) = = 1) & & (pattern2 (y-1 x) ~ =标志)
num_new2 = num_new2 + 1;
num_new2 new2 (1) = x;
num_new2 new2 (2) = y-1;
pattern2 (y-1 x) =国旗;
结束
如果(y <纽约)& &(模式(y + 1, x) = = 1) & & (pattern2 (y + 1, x) ~ =国旗)
num_new2 = num_new2 + 1;
num_new2 new2 (1) = x;
num_new2 new2 (2) = y + 1;
pattern2 (y + 1, x) =国旗;
结束
结束
num_new = num_new2;
新(:,1:num_new) = new2 (:, 1: num_new);
如果马克斯(新(2,1:num_new)) = =纽约
发现= 1;
打破;
结束
结束
结束
结束
numlink = numlink +发现;
结束
linked_list (sss) = numlink / num_sample;
结束
如果matlabpool (“大小”)> 0
matlabpool关闭
结束
情节(linked_list plist,‘*’)
toc