public
Last active

Timings and memory consumption for the singular value decomposition implemented in scipy. The script svd_memory.py generates a plot about the memory consumption (requires the package `memory_profiler`) and the second one will plot the timings. The scripts will perform several iterations for matrices of different sizes and will take about 30 minutes to complete.

  • Download Gist
svd_memory.py
Python
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
# .. Memory benchmarks for SciPy's Singular Value Decomposition ..
# .. Author: Fabian Pedregosa <fabian@fseoane.net>
 
import numpy as np
from scipy.sparse import linalg as splinalg
from scipy import sparse, linalg
import pylab as pl
from memory_profiler import memory_usage
 
dims = np.arange(500, 1500, 20)
n_iter = 3
LAPACK_mem = np.zeros((n_iter, dims.size))
ARPACK_mem = np.zeros((n_iter, dims.size))
 
for i_iter in range(n_iter):
for i_dim, k in enumerate(dims):
x = np.random.randn(k, k)
tmp = memory_usage((linalg.svd, (x,)))
LAPACK_mem[i_iter, i_dim] = np.max(tmp)
tmp = memory_usage((splinalg.svds, (x,)))
ARPACK_mem[i_iter, i_dim] = np.max(tmp)
 
 
def errorfill(x, y, yerr, color=None, alpha_fill=0.3, ax=None, label=None):
# helper function, stolen from http://tonysyu.github.com/plotting-error-bars.html
ax = ax if ax is not None else pl.gca()
if color is None:
color = ax._get_lines.color_cycle.next()
if np.isscalar(yerr) or len(yerr) == len(y):
ymin = y - yerr
ymax = y + yerr
elif len(yerr) == 2:
ymin, ymax = yerr
ax.plot(x, y, color=color, label=label)
ax.fill_between(x, ymax, ymin, color=color, alpha=alpha_fill)
 
errorfill(dims, LAPACK_mem.mean(0), LAPACK_mem.std(0), label='linalg.svd')
errorfill(dims, ARPACK_mem.mean(0), ARPACK_mem.std(0), label='sparse.linalg.svds')
pl.ylabel('Memory (in MB)')
pl.legend(loc='upper left')
pl.axis('tight')
pl.show()
svd_timing.py
Python
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
# .. Benchmarks for SciPy's Singular Value Decomposition ..
# .. Author: Fabian Pedregosa <fabian@fseoane.net>
 
import time
import numpy as np
from scipy.sparse import linalg as splinalg
from scipy import sparse, linalg
import pylab as pl
 
dims = np.arange(500, 1500, 20)
n_iter = 3
LAPACK_time = np.zeros((n_iter, dims.size))
ARPACK_time = np.zeros((n_iter, dims.size))
 
# .. we perform several runs for greater stability ..
for i_iter in range(n_iter):
for i_dim, k in enumerate(dims):
# .. create the data ..
x = np.random.randn(k, k)
t0 = time.time()
linalg.svd(x)
LAPACK_time[i_iter, i_dim] = time.time() - t0
t0 = time.time()
splinalg.svds(x, k-1)
ARPACK_time[i_iter, i_dim] = time.time() - t0
 
 
# .. plot the result ..
 
def errorfill(x, y, yerr, color=None, alpha_fill=0.3, ax=None, label=None):
# helper function, stolen from http://tonysyu.github.com/plotting-error-bars.html
ax = ax if ax is not None else pl.gca()
if color is None:
color = ax._get_lines.color_cycle.next()
if np.isscalar(yerr) or len(yerr) == len(y):
ymin = y - yerr
ymax = y + yerr
elif len(yerr) == 2:
ymin, ymax = yerr
ax.plot(x, y, color=color, label=label)
ax.fill_between(x, ymax, ymin, color=color, alpha=alpha_fill)
 
errorfill(dims, LAPACK_time.sum(0), LAPACK_time.std(0), label='linalg.svd')
errorfill(dims, ARPACK_time.sum(0), LAPACK_time.std(0), label='sparse.linalg.svds')
pl.ylabel('Time (in seconds)')
pl.xlabel('Size of matrices')
pl.legend(loc='upper left')
pl.axis('tight')
# pl.savefig('svd_timing.png')
pl.show()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.