In some specific case, preallocation is slower than doing nothing.

5 次查看(过去 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')?

采纳的回答

Stephen23
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.
  1 个评论
Sangmin Lee
Sangmin Lee 2021-3-7
Thanks!
Now I changed the code as below, and see the difference.
Probably preallocation only have evident advantage when the matrix is relatively large...
If I decrease num_mode to 100, the difference is negligible.
Thanks for the advice!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by