Performance comparison: mp vs. fp and "overwrite=False" vs. "overwrite=True"
It turns out that fp is 2x-3x faster than mp, while "overwrite=True" does not imporve!
In [1]:
import mpmath as mp
In [2]:
from eigen_markov import stoch_eig, gth_solve
In [3]:
sizes = [10, 30, 50, 100]
rand_matrices_mp = []
rand_matrices_mp_copy = [] # Will test "overwrite=True"
for n in sizes:
P = mp.randmatrix(n, n)
for i in range(n):
P[i, :] /= mp.fsum(P[i, :])
rand_matrices_mp.append(P)
rand_matrices_mp_copy.append(P.copy())
In [4]:
rand_matrices_fp = \
[mp.fp.matrix(P.tolist()) for P in rand_matrices_mp]
rand_matrices_fp_copy = \
[mp.fp.matrix(P.tolist()) for P in rand_matrices_mp_copy]
In [5]:
rand_matrices_mp == rand_matrices_fp
Out[5]:
In [6]:
type(rand_matrices_mp[0][0, 0])
Out[6]:
In [7]:
type(rand_matrices_fp[0][0, 0])
Out[7]:
In [8]:
import time
In [9]:
v0, v1 = [[], []], [[], []]
t0, t1 = [[], []], [[], []]
for P in rand_matrices_mp:
for i, overwrite in enumerate([False, True]):
start = time.time()
v = mp.mp.stoch_eig(P, overwrite=overwrite)
timediff = time.time() - start
v0[i].append(v)
t0[i].append(timediff)
for P in rand_matrices_fp:
for i, overwrite in enumerate([False, True]):
start = time.time()
v = mp.fp.stoch_eig(P, overwrite=overwrite)
timediff = time.time() - start
v1[i].append(v)
t1[i].append(timediff)
In [10]:
for i, (P, Q) in enumerate(zip(rand_matrices_mp_copy, rand_matrices_fp_copy)):
print 'rand_matrices[{0}] ({1} x {2})'.format(i, P.rows, P.cols)
print ' mp.mp.stoch_eig overwrite=F:', mp.norm(v0[0][i]*P - v0[0][i], 1), t0[0][i]
print ' mp.mp.stoch_eig overwrite=T:', mp.norm(v0[1][i]*P - v0[1][i], 1), t0[1][i]
print ' mp.fp.stoch_eig overwrite=F:', mp.norm(v1[0][i]*Q - v1[0][i], 1), t1[0][i]
print ' mp.fp.stoch_eig overwrite=T:', mp.norm(v1[1][i]*Q - v1[1][i], 1), t1[1][i]
In [11]:
for i, (P, Q) in enumerate(zip(rand_matrices_mp_copy, rand_matrices_fp_copy)):
print 'rand_matrices[{0}] ({1} x {2})'.format(i, P.rows, P.cols)
for overwrite in [False, True]:
%timeit mp.mp.stoch_eig(P, overwrite=overwrite)
for overwrite in [False, True]:
%timeit mp.fp.stoch_eig(Q, overwrite=overwrite)
In [11]: