[SciPy-dev] conversion among sparse formats

Nathan Bell wnbell@gmail....
Fri Dec 7 21:24:01 CST 2007


I've added a small benchmark to the scipy.sparse unittests (enabled with
level 5) that compares the cost of converting among the various sparse
formats.  The matrix is 900x900 with approximately 5 nonzeros per row
(5-point finite difference stencil).  Using the latest SVN, you can produce
the table below with the following command:
 python -c 'from scipy.sparse import test; test(5)'

                     Sparse Matrix Conversion
====================================================================
format |  tocsr()  |  tocsc()  |  tocoo()  |  tolil()  |  todok()
--------------------------------------------------------------------
 csr   |  0.000002 |  0.000292 |  0.000356 |  3.460000 |    n/a
 csc   |  0.000312 |  0.000002 |  0.000358 |  3.450000 |    n/a
 coo   |  0.000389 |  0.000361 |  0.000002 |  3.460000 |    n/a
 lil   |  0.024000 |  0.024000 |  0.022000 |  3.540000 |    n/a
 dok   |  0.027500 |  0.060000 |  0.025000 |  3.510000 |    n/a


The numbers are the time (in seconds) to convert a matrix in format on the
left to each of the formats using the to___() methods.

Some observations:
1) None of the formats supports todok().  Does anyone want/need this?

2) Something is definitely wrong with tolil() since the reverse conversions
(e.g. lil_matrix.tocsr()) are 100x faster.

3) Conversion among CSR/CSC/COO is relatively cheap, so it should be
leveraged when going between the 'slow' but flexible formats (lil and dok)
and the 'fast' but inflexible formats (csr/csc/coo).  Specifically, when
converting any 'fast' format to a 'slow' format, you should first convert to
the most convenient 'fast' format.  Conversely, when converting from a
'slow' to a 'fast' format, using another 'fast' format as an intermediate
incurs no noticable overhead.

4) lil_matrix and dok_matrix are the only construction-oriented formats we
have, so it's important that they be reasonably quick.

-- 
Nathan Bell wnbell@gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-dev/attachments/20071207/d658964b/attachment.html 


More information about the Scipy-dev mailing list