- Go to the grocery store, fill your shopping cart with all the items you need, buy them, and take them home.
- Go to the grocery store, buy one item, take it home. Go to the grocery store, buy the second item on your list, take it home. Go to the grocery store, buy the third item on your list, take it home. etc.
A question about the efficiency of matrix multiplication in MATLAB.
7 次查看(过去 30 天)
显示 更早的评论
If we have the following two matrices in MATLAB:
A=rand(100,100);
B=rand(100,1000);
Now we consider the following two cases of calculations:
(1) A*B, which is performed 10000 times:
tic
for k=1:10000
A*B;
end
toc
(2) A*B(:,1), A*B(:,2), ..., A*B(:,1000), each of which is performed 10000 times:
tic
for j=1:1000
b_j=B(:,j);
for k=1:10000
A*b_j;
end
end
toc
It is obvious that the total times of multiplication required in the two cases are the same. However, a comparison made in MATLAB indicates that the computational time used by case-1 is significantly less than that by case-2. Could someone please make some explanations or comments about this question?
0 个评论
回答(2 个)
Steven Lord
2021-11-9
If you were going to the grocery store to pick up a bunch of items for dinner, which process would be more efficient?
Your first example calls the * operator 10,000 times. Your second calls it 1000*10,000 times. Sure, that second example is calling the function on smaller arrays, but the fact that in your second shopping process your grocery bag will be lighter each trip doesn't outweigh the extra trips between your house and the store.
Chris
2021-11-9
Matlab was built to do matrix math first, with the highly optimized LAPACK library at its heart. That's what it's using for case 1. Matlab is not as optimized for iterating through loops.
Vectorized operations are faster than loops as well. If you need to apply the same set of operations to all elements of a multi-dimensional array, it is nearly always going to be faster if you can find a way to do it without loops. For example, the element-wise operators
.* ./ .^
and relational operators
< > <= >= == ~=
can operate on entire arrays without introducing loops, and are thus quite fast.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!