Why is scalar division slower than piece-wise matrix division?

7 vues (au cours des 30 derniers jours)
Simon Schiele
Simon Schiele le 15 Juin 2020
Hello,
today I explained to my student that piece-wise division is much faster than a corresponding operation realized through a loop. However, I had no idea how much. Obviously it depends on the size of the matix. I, therefore, wrote follwowing exemplary code:
lengths = round(logspace(0,7));
t1s = zeros(numel(lengths),1);
t2s = zeros(numel(lengths),1);
for j=1:numel(lengths)
tic %version a - piece-wise division
a = ones(lengths(j),3)./(2*ones(lengths(j),1));
t1s(j) = toc;
tic %version b - loop
a = ones(lengths(j),3);
for i=1:size(a,1)
a(i,:) = a(i,:) / 2;
end
t2s(j) = toc;
end
figure
hold on
ylabel('calc. time / sec');
xlabel('matrix length / -');
plot(lengths,t1s);
plot(lengths,t2s);
set(gca, 'XScale', 'log')
set(gca, 'YScale', 'log')
yyaxis right
set(gca, 'YScale', 'log')
plot(lengths,abs(t1s-t2s));
legend('Piece-wise','loop','delta');
The code divides increasing numbers of vectors by two. The result looks like this:
It comes as no surprise that piece-wise divsion is much faster than a for loop (Roughly factor 100).
But can somone please explain why both versions are relativley slow at 1<L<10?
Simon

Réponses (0)

Catégories

En savoir plus sur Measurements and Spatial Audio dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by