Slow "sum" on sparse matrices

8 views (last 30 days)
Sebastian
Sebastian on 12 Oct 2012
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

Answers (1)

Matt J
Matt J on 12 Oct 2012
Edited: Matt J on 12 Oct 2012
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.

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!