scipy/scipy — pull request #311 by jakevdp (2012-09-14 14:07:55+00:00)
Title: ENH: speed up csr._get_rowslice and allow striding

This is in response to PR #307

I've improved the csr_matrix code for getting slices of single rows. It's now much faster than before, and correctly handles slices with arbitrary strides. Here's a benchmark similar to that mentioned in #307:

``` python
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
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.getrow(i)
print "getrow(i)", (time.time() - st)/no_samples

st = time.time()
for i in idx:
b = a[i:i+1]
print "a[i:i+1]", (time.time() - st)/no_samples
```

These are the results on my machine:
```
Shape and nnz/size (500, 500) 0.799424
a[i] 0.000124758243561
getrow(i) 0.000647622108459
a[i:i+1] 0.000129615783691

Shape and nnz/size (500, 500) 0.399712
a[i] 0.000122797966003
getrow(i) 0.000641570091248
a[i:i+1] 0.000126832008362
```

In addition, tests pass for arbitrary positive and negative strides.