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!