This repository has been archived by the owner on Nov 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathplot_benchmarks.py
99 lines (73 loc) · 2.61 KB
/
plot_benchmarks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import numpy as np
import matplotlib.pyplot as plt
from time import time
from scipy.sparse import csc_matrix
def sparse_matrix(N1, N2, f, conversion=np.asarray, rseed=0):
"""create NxN matrix with an approximate fraction f of nonzero entries"""
rng = np.random.RandomState(rseed)
M = rng.rand(N1, N2)
M[M > f] = 0
return conversion(M)
def time_svd(svdfunc, N1, N2, f, rseed=0, bestof=3, args=None, matfunc=np.asarray, **kwargs):
if args is None:
args = ()
N1_N2_f = np.broadcast(N1, N2, f)
times = []
for (N1, N2, f) in N1_N2_f:
M = sparse_matrix(N1, N2, f, matfunc, rseed)
t_best = np.inf
for i in range(bestof):
t0 = time()
res = svdfunc(M, *args, **kwargs)
t1 = time()
t_best = min(t_best, t1 - t0)
times.append(t_best)
return np.array(times).reshape(N1_N2_f.shape)
def plot_propack(ax, N1, N2, f, k):
from pypropack import svdp
print "computing execution times for propack..."
t = time_svd(svdp, N1, N2, f, k=k, kmax=100,
matfunc=csc_matrix)
ax.plot(N1, t, label='propack (k=%i)' % k)
def plot_arpack(ax, N1, N2, f, k):
from scipy.sparse.linalg import svds
print "computing execution times for arpack..."
t = time_svd(svds, N1, N2, f, k=k, matfunc=csc_matrix)
ax.plot(N1, t, label='arpack (k=%i)' % k)
def plot_svdlibc(ax, N1, N2, f, k):
from sparsesvd import sparsesvd
print "computing execution times for svdlibc..."
t = time_svd(sparsesvd, N1, N2, f, args=(5,), matfunc=csc_matrix)
ax.plot(N1, t, label='svdlibc (k=%i)' % k)
def plot_lapack(ax, N1, N2, f, k):
from scipy.linalg import svd
print "computing execution times for lapack..."
t = time_svd(svd, N1, N2, f, full_matrices=False)
ax.plot(N1, t, label='lapack (full)')
if __name__ == '__main__':
N = 2 ** np.arange(3, 10)
f = 0.6
k = 5
fig, ax = plt.subplots(subplot_kw=dict(xscale='log', yscale='log'))
try:
plot_propack(ax, N, N, f, k)
except ImportError:
print "propack cannot be loaded"
try:
plot_arpack(ax, N, N, f, k)
except ImportError:
print "scipy arpack wrapper cannot be loaded"
try:
plot_svdlibc(ax, N, N, f, k)
except ImportError:
print "svdlibc cannot be loaded"
try:
plot_lapack(ax, N, N, f, k)
except ImportError:
print "scipy lapack wrapper cannot be loaded"
ax.legend(loc=2)
ax.set_xlabel('N')
ax.set_ylabel('t (s)')
ax.set_title('Execution Times for k=5')
ax.grid(color='gray')
plt.show()