
Why is the built-in dot product function somewhat inefficient?
7 views (last 30 days)
Show older comments
I implemented dot product operation using the definition and a for-loop. I did not expect it to be faster than the built-in function, but it appears to be a lot faster. (There are other faster ones; I tried the brute force way for comparison purposes. Meh) So I checked the source code for built-in dot(), it seems to be calling another built-in function sum(). However, the source code for sum does not seem to be visible. Expecting the built-in dot product product to be the fastest since I'm using it rather frequently, would anyone please explain why?
1 Comment
Cedric
on 6 Oct 2017
Edited: Cedric
on 6 Oct 2017
Could you post a test case and the way you benchmark both approaches?
Many MATLAB functions introduce some overhead because they test their inputs, sometimes choose the best method for solving the problem (e.g. linsolve). When you perform a lot of calls applied to very small arrays, they can easily be slower than a "hand-made" specific code that implements no test. Usually though, we are working on rather large arrays (the objective is often to work on the largest arrays possible and perform as few operations as possible), and the overhead is negligible.
Just to illustrate, if you save the following in M-File main.m:
N = 1e5 ;
for k = 1 : N
a = dot( [1,2,3], [1;2;3] ) ;
end
and then open the profile and run it:
>> profile viewer
type main in the text filed "Run this code" and click on [Start Profiling], you will see clicking on dot in the report where most of the time is spent.

(and if you scroll down you have the code highlighted) Here you see that it would be more efficient to implement the operation by yourself.
Accepted Answer
James Tursa
on 6 Oct 2017
Edited: James Tursa
on 6 Oct 2017
Other than the supposition that dot() needs to be more generic for the multi-dimensional cases it must cover, the "why" is unknown. But this has been observed and reported on this forum and on the newsgroup several times before. The cross() function is another example of a supplied MATLAB function that can easily be beat for speed by simple hand-written m-code in simple cases.
If it is a bottleneck for timing in your application, go ahead and use your own hand-written code. That's what I do. For 1D vectors, simply writing the result as a matrix multiply would be preferred. E.g., an example with very large vectors:
>> format long g
>> v = rand(10000000,1);
>> w = rand(10000000,1);
>> tic;disp(dot(v,w));toc
2498633.9177071
Elapsed time is 0.060657 seconds.
>> tic;disp(v'*w);toc
2498633.91770709
Elapsed time is 0.007192 seconds.
In the latter case, the multiply itself is passed off to a very fast highly optimized BLAS library routine in the background.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!