对数组进行预分配后,反而严重降低了赋值的速度,请问如何解决?
25 views (last 30 days)
Show older comments
数组预分配后,赋值操作反而需要更多的时间,矩阵的维度越大,预分配后进行赋值需要的时间更多。
注释掉预分配语句后,赋值时间减少
clearvars
test_h_matx_diag = zeros(1024,1024,360); % 预分配语句
h_matx = rand(1024,1,360);
h_matx = exp(-1j*2*pi*h_matx);
for ii = 1:size(h_matx,3)
temp1 = diag(h_matx(:,:,ii));
test_h_matx_diag(:,:,ii) = temp1; % 赋值的速度受预分配的影响
end
0 Comments
Accepted Answer
埃博拉酱
on 26 Oct 2024 at 14:21
Edited: 埃博拉酱
on 26 Oct 2024 at 14:31
我测试的结果是预分配7秒,不预分配11秒,并没有出现预分配反而更慢的问题。
你是否在进行不预分配的测试之前,忘记了清空上次预分配测试后残余的变量?
另外,这样做更快,只要2秒:
test_h_matx_diag = zeros(1024*1024,360); % 预分配语句
test_h_matx_diag(1:1025:end,:)=exp(-1j*2*pi*rand(1024,360));
test_h_matx_diag=reshape(test_h_matx_diag,1024,1024,360);
3 Comments
More Answers (0)
See Also
Categories
Find more on Big Data Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!