# [Numpy-discussion] sparse matrix dot product

Nicolas Rougier Nicolas.Rougier@loria...
Fri May 29 06:52:16 CDT 2009

```Hi,

I tried to post results but the file is too big, anyway, here is the
benchmark program if you want to run it:

Nicolas

-----

import time
import numpy
from scipy import sparse

def benchmark(xtype = 'numpy.array', xdensity = 0.1,
ytype = 'numpy.array', ydensity = 1.0, n = 1000):

x = numpy.zeros((n,n), dtype = numpy.float64)
xi = int(n*n*xdensity)
x.reshape(n*n)[0:xi] = numpy.random.random((xi,))
y = numpy.zeros((n,1), dtype = numpy.float64)
yi = int(n*ydensity)
y.reshape(n)[0:yi] = numpy.random.random((yi,))
x = eval('%s(x)' % xtype)
y = eval('%s(y)' % ytype)
t0 = time.clock()
if xtype == 'numpy.array' and ytype == 'numpy.array':
for i in range(1000):
z = numpy.dot(x,y)
else:
for i in range(1000):
z = x*y
tf = time.clock() - t0
text = ''
text += (xtype + ' '*20)[0:20]
text += (ytype + ' '*20)[0:20]
text += '%4dx%4d   %4dx%4d   %.2f        %.2f        %.2f' %
(n,n,n,1,xdensity, ydensity, tf)
return text

xtypes = ['numpy.array',
'numpy.matrix',
'sparse.lil_matrix',
'sparse.csr_matrix',
'sparse.csc_matrix']
ytypes = ['numpy.array',
'numpy.matrix',
'sparse.lil_matrix',
'sparse.csr_matrix',
'sparse.csc_matrix']
xdensities = [0.01, 0.10, 0.25, 0.50, 1.00]
ydensities = [1.00]

print '=================== =================== =========== ===========
=========== =========== ======='
print 'X type              Y type              X size      Y size      X
density   Y density   Time   '
print '------------------- ------------------- ----------- -----------
----------- ----------- -------'
n = 100
for xdensity in xdensities:
for ydensity in ydensities:
for xtype in xtypes:
for ytype in ytypes:
print benchmark(xtype, xdensity, ytype, ydensity, n)
print '------------------- ------------------- -----------
----------- ----------- ----------- -------'
n = 1000
for xdensity in xdensities:
for ydensity in ydensities:
for xtype in xtypes:
for ytype in ytypes:
print benchmark(xtype, xdensity, ytype, ydensity, n)
print '------------------- ------------------- -----------
----------- ----------- ----------- -------'
print '=================== =================== =========== ===========
=========== =========== ======='

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090529/855da2c0/attachment.html
```