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

In [2]:
with open("f1.txt") as f:
    i = []
    j = []
    for line in f:
        fd = line.strip().split()
        i.append(int(fd[0]))
        j.append(int(fd[1]))
        j.append(int(fd[0]))
        i.append(int(fd[1]))

N = 626892
F1 = coo_matrix((np.arange(len(i)), (i, j)), shape=(N, N))

F1.nnz


Out[2]:
86614

In [3]:
with open("f2.txt") as f:
    i = []
    j = []
    for line in f:
        fd = line.strip().split()
        i.append(int(fd[0]))
        j.append(int(fd[1]))
        j.append(int(fd[0]))
        i.append(int(fd[1]))

N = 626892
F2 = coo_matrix((np.arange(len(i)), (i, j)), shape=(N, N))
F2.nnz


Out[3]:
481466

In [4]:
F1 = F1.tocsr()
F2 = F2.tocsr()
F1.nnz, F2.nnz


Out[4]:
(83944, 481466)

In [5]:
%time

S = F1 + F1
S.nnz


CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 19.1 µs
Out[5]:
83943

In [6]:
%time

S = (F1+F2) * (F1+F2)
S.nnz


CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 6.44 µs
Out[6]:
1534169693