(満足のいく回答が得られなかったので再投稿し、もう誰も気にしていないようです)ここに問題があります
次の関数を検討してください。
function A = plodding(N,d)
for ii = 1:N
jj = 1;
A(ii,jj) = randn;
while abs(A(ii,jj)) < d
jj = jj + 1;
A(ii,jj) = randn;
end
end
この関数を書き直して、速度を低下させている割り当ての問題を解消します。新しい関数 cruising を呼び出します。使用可能なメモリが 7.8 ギガバイトの Dell Latitude E6410 では、割り当ての問題を解消すると、速度が 7 倍向上します。
これが私の仕事です:
rng(0) を使用した元のバージョン
function A = plodding(N,d)
rng(0); % To compare between the original and the modified version
for ii = 1:N
jj = 1;
A(ii,jj) = randn;
while abs(A(ii,jj)) < d
jj = jj + 1;
A(ii,jj) = randn;
end
end
end
修正版
function A = cruising(N,d)
rng(0);
for jj = 1:N % Reorganize, so elems are added column-wise
ii = 1;
A(ii,jj) = randn;
while abs(A(ii,jj)) < d
ii = ii + 1;
A(ii,jj) = randn;
end
end
A = A'; % To get the matrix desired
end
しかし、テストすると(私の質問を理解するために結果を注意深く見てください)
>>tic;A = plodding(10000,2);toc
Elapsed time is 9.289355 seconds.
>>tic;A1 = cruising(10000,2);toc;
Elapsed time is 0.078783 seconds.
>>
>>tic;A = plodding(5,5);toc
Elapsed time is 1.168961 seconds.
>> tic;A1 = cruising(5,5);toc;
% When I posted this thread, it's already more than 10 mins and MATLAB's still "busy"!
レッスンから学んだことに基づいて、MATLAB は要素を列単位で格納するため、私のロジックは正しいはずです。したがって、N が小さいか大きいか、d が小さいか大きいかのいずれの場合でも、クルージングは、いずれの場合でも、ずるずる走るよりも高速に実行する必要があります。MATLAB のこの動作は、私がまだよく理解していない何かがあることを示唆しています。誰か助けてくれませんか???