In some specific case, preallocation is slower than doing nothing.
2 次查看(过去 30 天)
显示 更早的评论
Hi,
Please see the below screenshot. I think in this specific case, preallocation does not help.
for k=1:5
clearvars
sig_smpl = 10001;
num_mode = 3;
tic
for i=1:100
eta = -0.5 + rand(sig_smpl,1); % -0.5 ~ 0.5 random generation from uniform distribution
h = -0.5 + rand(sig_smpl,1);
for j=1:num_mode
G_U(:, j) = conv(eta, h);
G_L(:, j) = conv(h, eta);
end
end
toc
end
%%
for k=1:5
clearvars
sig_smpl = 10001;
num_mode = 3;
G_U = zeros(sig_smpl * 2 - 1, num_mode);
G_L = zeros(sig_smpl * 2 - 1, num_mode);
tic
for i=1:100
eta = -0.5 + rand(sig_smpl,1); % -0.5 ~ 0.5 random generation from uniform distribution
h = -0.5 + rand(sig_smpl,1);
for j=1:num_mode
G_U(:, j) = conv(eta, h);
G_L(:, j) = conv(h, eta);
end
end
toc
end
What did I wrong?
p.s. Is there any way copy and past of my live script to here with results ('Output')?
2 个评论
采纳的回答
Stephen23
2021-3-7
编辑:Stephen23
2021-3-7
"What did I wrong?"
Your timing comparison is 99% meaningless.
Your are comparing the times of 100 loop iterations (the i loop), but only on the first loop iteration does the array get expanded by the j loop, after that the G_U and G_L matrices already exist and their content will simply get updated. So for the other 99 loop iterations that you are timing (of the i loop) there is practically no difference between your two versions.
You will not get a meaningful comparison of that first loop (the only one where preallocation makes any difference), when its timing is merged in with 99 other loop iterations (where both versions are effectively preallocated, thus no meaningful difference in your comparison). In the end you are basically measuring OS noise of all 100 iterations, because for your small arrays that swamps everything else.
The i loop seems to achieve nothing anyway. Did you add it as an attempt to compare the timing?
For that matter, the j loop does not seem to serve any purpose either.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!