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

2 ビュー (過去 30 日間)
Sangmin Lee
Sangmin Lee 2021 年 3 月 7 日
コメント済み: Sangmin Lee 2021 年 3 月 7 日
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 7 日
Don't use clearvars in a loop: it interferes with optimization.
Sangmin Lee
Sangmin Lee 2021 年 3 月 7 日
@Walter Roberson Thanks for the advice!

サインインしてコメントする。

採用された回答

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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by