vecnorm seems slower than it should be
8 views (last 30 days)
Show older comments
In the speed comparison below, I would expect vecnorm() to be faster than the first version, because the first version creates an A-sized temporary matrix. However, vecnorm() is significantly slower. Any thoughts as to why this is?
A=rand(1e4,1000)-0.5;
tic
x1=mean(abs(A),2);
toc;
tic;
x2=vecnorm(A,1,2)./size(A,2);
toc
0 Comments
Accepted Answer
Jan
on 7 Mar 2022
Edited: Jan
on 7 Mar 2022
I can confirm your observation.
A = rand(1e4, 1000) - 0.5;
tic
for k = 1:50
x1 = mean(abs(A),2);
end
toc; % Overhead of the function MEAN is measureable:
tic
for k = 1:50
x1 = sum(abs(A), 2) / size(A, 2);
end
toc; % A direct SUM(X)/LENGTH is faster than MEAN:
tic;
for k = 1:50
x3 = vecnorm(A, 1, 2) / size(A, 2);
end
toc % VECNORM(X, 1) is slower:
tic;
for k = 1:50
x3 = vecnorm(A, 2, 2) / size(A, 2);
end
toc % Obviously VECNORM(X, 2) is optimized: It shoud be slower due to the SQRT:
6 Comments
Bruno Luong
on 7 Mar 2022
On my poor old laptop
sqrt: Elapsed time is 0.744571 seconds.
vecnorm: Elapsed time is 1.417963 seconds.
Jan
on 7 Mar 2022
A = rand(1e4, 1000) - 0.5;
tic;
for k = 1:50
x3 = vecnorm(A, 2, 2);
end
toc
% Elapsed time is 3.662537 seconds.
tic;
for k = 1:50
x4 = DNorm2(A, 2);
end
toc
% Elapsed time is 0.427130 seconds.
This is a single-threaded C-mex. The only trick is, that columnwise operations are preferred until some heuristic limits. Calling optimized BLAS routines should slightly faster.
vecnorm was improved since R2018b. For a fair comparison:
N = 10000;
A = rand(N,2);
D = permute(A,[1,3,2])-permute(A,[3,1,2]);
tic
B = sqrt(sum(D.^2,3));
toc % Elapsed time is 1.817327 seconds.
tic
C = DNorm2(D, 3);
toc % Elapsed time is 0.829278 seconds.
More Answers (0)
See Also
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!