MATLAB: Is the built-in dot product function somewhat inefficient

dot productefficiency

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?

Best Answer

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.