把循环并行化。或者优化循环体中的代码是最常常使用的加快程序执行速度的思路。编程
比例如如下方代码中a就是简约变量。 性能
a = 0; for i = 1:1000 a = a+i; end
+ - * .* & | [,] [;] {,} {;} min max union intersect
。* [] {}
底层有特殊处理保证结果的正确性。优化
a[i] a[i+1]
同一时候出现则a不被识别为切片变量。不影响parfor以前声明的同名变量。spa
tmp = 5; broadcast = 1; reduced = 0; sliced = ones(1, 10); parfor i = 1:10 tmp = i; reduced = reduced + i + broadcast; sliced(i) = sliced(i) * i; end
matlabpool n
命令可以打开n个worker。matlabpool open configname
依照指定配置打开,默认配置为local
。matlabpool close
关闭worker。Parallel -> Manage Cluster Profile
完毕。c-1
。对于计算密集型程序,超线程带来的性能提高差点儿为0,可以设置为核心数,而不是线程数。.net
尤为对于广播变量。假设较大可尝试变为切片变量。线程