Python: multiplication of sparse matrices slower in csr_matrix than numpy I am trying to multiply a sparse matrix with itself using numpy and scipy.sparse.csr_matrix. The size of matrix is 128x256. Its 93% values are 0. Ironically the multiplication using numpy is faster than scipy.sparse. I do not know why? The code I am using is:

import numpy,time
W=numpy.random.choice([0, 1], size=(128,256), p=[0.93,0.07])
start=time.time()
W1=numpy.matmul(W,numpy.transpose(W))
end=time.time()
print(end-start)

from scipy.sparse import csr_matrix
start=time.time()
W1=csr_matrix(W).dot(csr_matrix(W).transpose())
end=time.time()
print(end-start)


Numpy gives time 0.0006 and scipy gives 0.004. Why. Comparing times for dense matrix, numpy gives smaller time on dense matrix as well and scipy takes more time. Why is the time for scipy.sparse not less than numpy for sparse matrix
Here, you do not time only the time taken to make the matrix multiplication but also the time taken to convert your matrix from dense to sparse. If you convert your matrix before the timing starts, you will see that multiplication with scipy is indeed more than twice faster

import time, numpy, scipy
from scipy.sparse import csr_matrix
import numpy as np

W = np.random.binomial(n=1, p=0.01, size=(100, 100))
start=time.time()
numpy.matmul(W,numpy.transpose(W))
end=time.time()
dt_dense = end - start 
print ('time taken for the dense matrix {}'.format(end - start))

sparse_W = csr_matrix(W)
start=time.time()
sparse_W.dot(sparse_W.transpose())
end=time.time()
dt_sparse = end - start
print ('time taken for the sparse matrix {}'.format(end - start))
dt_dense/dt_sparse