[SciPy-dev] unexpectedly slow norm()

Nathan Bell wnbell@gmail....
Wed Mar 19 02:22:25 CDT 2008

I use norm() frequently in my own codes and I was recently surprised
to see it near the top of my profiling results.  It seems that both
SciPy and NumPy incur a large amount of overhead in this operation.

Consider the following script

from time import clock
from numpy import inner, sqrt, ones
import numpy.linalg
import scipy.linalg
from scipy.lib.blas import get_blas_funcs

x = ones(5*1000*1000)

scipy_norm = scipy.linalg.norm
numpy_norm = numpy.linalg.norm
def adhoc_norm(x):
    return sqrt(inner(x,x))
blas_norm = get_blas_funcs(('nrm2',),(x,))[0]

for fn in [scipy_norm, numpy_norm, adhoc_norm, blas_norm ]:
    start = clock()
    for i in range(10):
        n = fn(x)
    end = clock()
    print "%f seconds per call"  % ((end - start)/10.0)

Which outputs the following on my laptop:

0.407000 seconds per call
0.089000 seconds per call
0.013000 seconds per call
0.012000 seconds per call

Nathan Bell wnbell@gmail.com

More information about the Scipy-dev mailing list