Slow "sum" on sparse matrices

조회 수: 7 (최근 30일)
Sebastian
Sebastian 2012년 10월 12일
Hi,
I'm having trouble understanding the performance of the "sum" function, when dealing with sparse matrices.
Here's my minimalistic test-case:
function testsparse_sum()
r1 = sprand(1e6,1,1e-3);
r2 = sprand(1e6,1,1e-3);
for i=1:1000
r = [r1,r2];
r_sum1 = r1+r2;
r_sum2 = sum(r,2);
if ~isequal(r_sum1, r_sum2)
error('');
end
end
end
Now the profiler tells me, that while r_sum1 takes 0.03 seconds in total, r_sum2 takes ~7 seconds.
Am I missing something obvious here or this the expected behaviour? As I couldn't find any sparse-specific version of "sum", I assumed that the ordinary sum would work properly as well.
Cheers, sebastian
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 10월 12일
Is "r" still a sparse matrix?

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2012년 10월 12일
편집: Matt J 2012년 10월 12일
I think part of it is that sum() has to be coded to handle a wider variety of situations and so is harder to optimize. sum(...,2) doesn't know in advance how may columns it has to sum across, so it needs to use loops over columns, define loop variables, increment pointers, etc...
Conversely, PLUS(a,b) always knows it will have exactly 2 summands and can take advantage of that foreknowledge. It only has to loop in 1 dimension, for one thing,....
If you repeat your experiment with non-sparse array types, you'll see that sum() is also significantly slower, though by a smaller margin than for sparse.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by