【MATLAB基础】并行计算

打开并行计算功能

>> parpool
Starting parallel pool (parpool) using the 'local' profile ...
Connected to the parallel pool (number of workers: 8).
ans = 
 ProcessPool with properties: 

            Connected: true
           NumWorkers: 8
              Cluster: local
        AttachedFiles: {}
    AutoAddClientPath: true
          IdleTimeout: 30 minutes (30 minutes remaining)
          SpmdEnabled: true

parfor

单重循环

%采用parfor进行并行计算
n = 200;
A = 500;
a = zeros(1,n);
tic
parfor ii = 1:n
    a(ii) = max(abs(eig(rand(A))));
end
toc
Elapsed time is 5.532609 seconds.

双重循环

外层parfor

N1 = 200;
N2 = 500;
a = rand(N1,N2);
b = rand(N1,N2);
tic
parfor ii = 1:N1
    for jj= 1:N2
    b(ii,jj) = a(ii,jj)+1;
    end
end
toc
Elapsed time is 0.444865 seconds.

内层parfor

N1 = 200;
N2 = 500;
a = rand(N1,N2);
b = rand(N1,N2);
tic
for ii = 1:N1
    parfor jj= 1:N2
    b(ii,jj) = a(ii,jj)+1;
    end
end
toc
Elapsed time is 18.886706 seconds.

可见外层parfor执行效率远高于内层parfor的效率。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值