Why is vector dot product slower with scipy's sparse csr_matrix than numpy's dense array?

蹲街弑〆低调 提交于 2019-12-24 00:16:28

问题


I have a situation in which I need to extract a single row from a sparse matrix and take its dot product with a dense row. Using scipy's csr_matrix, this appears to be significantly slower than using numpy's dense array multiplication. This is surprising to me because I expected that sparse dot product would involve significantly fewer operations. Here is an example:

import timeit as ti

sparse_setup = 'import numpy as np; import scipy.sparse as si;' + \
               'u = si.eye(10000).tocsr()[10];' + \
               'v = np.random.randint(100, size=10000)'

dense_setup  = 'import numpy as np; u = np.eye(10000)[10];' + \
               'v = np.random.randint(100, size=10000)'

ti.timeit('u.dot(v)', setup=sparse_setup, number=100000)
2.788649031019304

ti.timeit('u.dot(v)', setup=dense_setup, number=100000)
2.179030169005273

For matrix-vector multiplication, the sparse representation wins hands down, but not in this case. I tried with csc_matrix, but performance is even worse:

>>> sparse_setup = 'import numpy as np; import scipy.sparse as si;' + \
...                'u = si.eye(10000).tocsc()[10];' + \
...                'v = np.random.randint(100, size=10000)'
>>> ti.timeit('u.dot(v)', setup=sparse_setup, number=100000)
7.0045155879925005

Why does numpy beat scipy.sparse in this case? Is there a matrix format that's faster for these kind of computations?


回答1:


The CSR/CSC vector product call has a few microsecond overhead per call, from executing a tiny bit of Python code, and dealing with arguments in compiled code (scipy.sparse._sparsetools.csr_matvec).

On modern processors, computing vector dot products is very fast, so the overheads dominate the computing time in this case. Matrix-vector products itself is more expensive, and here a similar overhead is not visible.

Why are then the overheads smaller for Numpy? This is mainly just due to better optimization of the code; the performance of csr_matrix can likely be improved here.



来源:https://stackoverflow.com/questions/33817189/why-is-vector-dot-product-slower-with-scipys-sparse-csr-matrix-than-numpys-den

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!