In some specific case, preallocation is slower than doing nothing.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sangmin Lee
el 7 de Mzo. de 2021
Comentada: Sangmin Lee
el 7 de Mzo. de 2021
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 comentarios
Walter Roberson
el 7 de Mzo. de 2021
Don't use clearvars in a loop: it interferes with optimization.
Respuesta aceptada
Stephen23
el 7 de Mzo. de 2021
Editada: Stephen23
el 7 de Mzo. de 2021
"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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!