eigen_markov: Illustration 2

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]:
True

In [6]:
type(rand_matrices_mp[0][0, 0])


Out[6]:
mpmath.ctx_mp_python.mpf

In [7]:
type(rand_matrices_fp[0][0, 0])


Out[7]:
float

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]


rand_matrices[0] (10 x 10)
  mp.mp.stoch_eig overwrite=F: 6.93889390390723e-17 0.0100340843201
  mp.mp.stoch_eig overwrite=T: 6.93889390390723e-17 0.00910806655884
  mp.fp.stoch_eig overwrite=F: 1.11022302462516e-16 0.00322794914246
  mp.fp.stoch_eig overwrite=T: 1.11022302462516e-16 0.00313091278076
rand_matrices[1] (30 x 30)
  mp.mp.stoch_eig overwrite=F: 7.63278329429795e-17 0.21993803978
  mp.mp.stoch_eig overwrite=T: 7.63278329429795e-17 0.199735879898
  mp.fp.stoch_eig overwrite=F: 1.45716771982052e-16 0.0754089355469
  mp.fp.stoch_eig overwrite=T: 1.45716771982052e-16 0.0749809741974
rand_matrices[2] (50 x 50)
  mp.mp.stoch_eig overwrite=F: 9.36750677027476e-17 0.771180152893
  mp.mp.stoch_eig overwrite=T: 9.36750677027476e-17 0.778090953827
  mp.fp.stoch_eig overwrite=F: 2.81025203108243e-16 0.34636592865
  mp.fp.stoch_eig overwrite=T: 2.81025203108243e-16 0.340687990189
rand_matrices[3] (100 x 100)
  mp.mp.stoch_eig overwrite=F: 7.45931094670027e-17 6.27766299248
  mp.mp.stoch_eig overwrite=T: 7.45931094670027e-17 6.32000899315
  mp.fp.stoch_eig overwrite=F: 3.05311331771918e-16 2.70071816444
  mp.fp.stoch_eig overwrite=T: 3.05311331771918e-16 2.69610691071

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)


rand_matrices[0] (10 x 10)
100 loops, best of 3: 7.14 ms per loop
100 loops, best of 3: 7.06 ms per loop
100 loops, best of 3: 3.23 ms per loop
100 loops, best of 3: 3.23 ms per loop
rand_matrices[1] (30 x 30)
1 loops, best of 3: 164 ms per loop
10 loops, best of 3: 168 ms per loop
10 loops, best of 3: 74.6 ms per loop
10 loops, best of 3: 76.1 ms per loop
rand_matrices[2] (50 x 50)
1 loops, best of 3: 782 ms per loop
1 loops, best of 3: 785 ms per loop
1 loops, best of 3: 335 ms per loop
1 loops, best of 3: 331 ms per loop
rand_matrices[3] (100 x 100)
1 loops, best of 3: 6.06 s per loop
1 loops, best of 3: 6.13 s per loop
1 loops, best of 3: 2.71 s per loop
1 loops, best of 3: 2.76 s per loop

In [11]: