# Why stock DOT function is suboptimally implemented

6 views (last 30 days)
Bruno Luong on 20 Oct 2020
Edited: Jan on 20 Oct 2020
Followup the discussion in
and
and do so tic/toc and I think the option selected by TMW (R2020b) is suboptimal in term of speed for complex data as show as this benchmark that one can speed up almost 10 times (!):
z=randn(1,1e7)+1i*randn(1,1e7);
tic
p1=dot(z,z);
toc % Elapsed time is 0.162930 seconds.
tic
p2 = sum(conj(z).*z);
toc % Elapsed time is 0.092252 seconds.
tic
p3=z(:)'*z(:); % (:) copy data ?
toc % Elapsed time is 0.100122 seconds.
tic
zc=z(:); % (:) copy data ?
p4=zc'*zc;
toc % Elapsed time is 0.053257 seconds.
tic
zr=reshape(z,[],1); % reshape does not?
p5=zr'*zr;
toc % Elapsed time is 0.016140 seconds.
Any comment would be welcome.
Just to have a formal question: what is the reason of TMW choice?

Jan on 20 Oct 2020
Edited: Jan on 20 Oct 2020
Just a comment: Under Matlab 2018b, Core i5-3320M CPU @ 2.60GHz:
Elapsed time is 0.249669 seconds. dot(z,z)
Elapsed time is 0.168906 seconds. sum(conj(z).*z)
Elapsed time is 0.204284 seconds. z(:)'*z(:)
Elapsed time is 0.114597 seconds. zc=z(:); p4=zc'*zc
Elapsed time is 0.018371 seconds. zr=reshape(z,[],1); p5=zr'*zr
When I copy the code into a function and repeat the calls 5 times in a FOR loop:
Elapsed time is 0.865391 seconds.
Elapsed time is 0.844665 seconds.
Elapsed time is 1.021389 seconds.
Elapsed time is 0.484573 seconds.
Elapsed time is 0.047871 seconds.