Skip to content

Instantly share code, notes, and snippets.

@fabianp
Created December 10, 2012 14:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fabianp/4250756 to your computer and use it in GitHub Desktop.
Save fabianp/4250756 to your computer and use it in GitHub Desktop.
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 minu…
# .. 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()
# .. 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment