In [1]:
import numpy as np
from scipy.sparse import csr_matrix

In [2]:
a = np.eye(1000, dtype=np.int64)
print(type(a))


<class 'numpy.ndarray'>

In [3]:
print(a[:10, :10])


[[1 0 0 0 0 0 0 0 0 0]
 [0 1 0 0 0 0 0 0 0 0]
 [0 0 1 0 0 0 0 0 0 0]
 [0 0 0 1 0 0 0 0 0 0]
 [0 0 0 0 1 0 0 0 0 0]
 [0 0 0 0 0 1 0 0 0 0]
 [0 0 0 0 0 0 1 0 0 0]
 [0 0 0 0 0 0 0 1 0 0]
 [0 0 0 0 0 0 0 0 1 0]
 [0 0 0 0 0 0 0 0 0 1]]

In [4]:
print(a.shape)


(1000, 1000)

In [5]:
csr = csr_matrix(a)
print(type(csr))


<class 'scipy.sparse.csr.csr_matrix'>

In [6]:
def get_size_of_csr(csr):
    return csr.data.nbytes + csr.indices.nbytes + csr.indptr.nbytes

In [7]:
print(a.nbytes)


8000000

In [8]:
print(get_size_of_csr(csr))


16004

In [9]:
%%timeit
a @ a


2.19 s ± 90.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [10]:
%%timeit
csr * csr


135 µs ± 5.34 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [11]:
a_dense = np.random.randint(1, 100, (1000, 1000))
csr_dense = csr_matrix(a_dense)

In [12]:
print(a_dense[:10, :10])


[[98 46 10 41 53 34 82 91 77 82]
 [30 21 18 89 30 20 60 88 54  5]
 [44 91 92 34 21 75 55 21 60 44]
 [90 30 57 52 14  5 64 62 41  7]
 [59 37 79 21 37 77 45 74 43 61]
 [10 73 92 98 39 72 72  4 82 81]
 [26 48 64 93 81 78  3  6 52 52]
 [28 85 42 19 51 61 23 81 89 79]
 [32 78 24 84 28 67 80 38 85 64]
 [98 11 48 85 17  5 87 30 90 22]]

In [13]:
print(a_dense.dtype)


int64

In [14]:
print(a_dense.shape)


(1000, 1000)

In [15]:
print(a_dense.nbytes)


8000000

In [16]:
print(get_size_of_csr(csr_dense))


12004004

In [17]:
%%timeit
a_dense @ a_dense


2.19 s ± 244 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [18]:
%%timeit
csr_dense * csr_dense


1.91 s ± 123 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [19]:
a_10_10 = np.eye(10, dtype=np.int64)
csr_10_10 = csr_matrix(a_10_10)

In [20]:
print(a_10_10.nbytes)


800

In [21]:
print(get_size_of_csr(csr_10_10))


164

In [22]:
%%timeit
a_10_10 @ a_10_10


2.23 µs ± 421 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [23]:
%%timeit
csr_10_10 * csr_10_10


112 µs ± 455 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [24]:
a_100_100 = np.eye(100, dtype=np.int64)
csr_100_100 = csr_matrix(a_100_100)

In [25]:
print(a_100_100.nbytes)


80000

In [26]:
print(get_size_of_csr(csr_100_100))


1604

In [27]:
%%timeit
a_100_100 @ a_100_100


49.3 µs ± 4.32 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [28]:
%%timeit
csr_100_100 * csr_100_100


115 µs ± 790 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [29]:
a_200_200 = np.eye(200, dtype=np.int64)
csr_200_200 = csr_matrix(a_200_200)

In [30]:
print(a_200_200.nbytes)


320000

In [31]:
print(get_size_of_csr(csr_200_200))


3204

In [32]:
%%timeit
a_200_200 @ a_200_200


6.08 ms ± 126 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [33]:
%%timeit
csr_200_200 * csr_200_200


132 µs ± 14.9 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)