加速和内存优化循环

时间:2014-02-22 01:10:12

标签: performance matlab memory

我有以下数据类型:

K1,K2,K3,K4 = ... % predefined constants which are not subject to change.  
time = 1e6; % time steps length.
z = 1e3; % location vector length.
A_Sink = rand(1,time);
B_Sink = rand(1,time);
laserA = zeros(1,z);
laserB = zeros(1,z);
laserA_Old = zeros(1,z);
laserB_Old = zeros(1,z);

现在这里是代码:

for ii = 1:time
    laserA_Old = laserA;
    laserB_Old = laserB;
    laserA(2:end) = laserA_Old(2:end) + K1*diff(laserA_Old) + K2*laserB_old(2:end);
    laserA(1) = A_Sink(ii);        
    laserB(1:end-1) = laserB_Old(1:end-1) + K3*diff(laserB_Old) + K4*laserA_Old(1:end-1);
    laserB(end) = B_Sink(ii);
end 

现在我的主要抱怨是,我在分析器中看到,(2:end)(1:end-1)循环中的行正在分配内存而不释放它。这会导致MATLAB慢得多。 有没有办法加速事情并改善记忆?

非常感谢。

0 个答案:

没有答案