scipy/scipy — pull request #307 by stefanv (2012-09-11 17:49:13+00:00)
Title: ENH: Optimize sparse csr A[i,:] and A[i].

Georgiana Dinu provided me with a benchmark showing that row slicing into a sparse csr matrix is rather slow. This patch provides a shortcut for that specific use case, yielding a 10x speed improvement.

```
import numpy as np
import scipy.sparse as sp
import time
import random

a = sp.csr_matrix(np.mat(np.random.rand(500,500)))

no_samples = 500
idx = range(no_samples)
random.shuffle(idx)

for thr in [0.2, 0.6]:

a.data[a.data < thr] = 0
a.eliminate_zeros()
print "Shape and nnz/size", a.shape, a.nnz/float(a.shape[0]*a.shape[1])

st = time.time()
for i in idx:
b = a[i]

print "a[i]", (time.time() - st)/no_samples

st = time.time()
for i in idx:
b = a[i, :]

print "a[i,:]", (time.time() - st)/no_samples

st = time.time()
for i in idx:
b = a.getrow(i)

print "getrow(i)", (time.time() - st)/no_samples
```