Why is the built-in dot product function somewhat inefficient?
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
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
More Answers (0)
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!