A Monday 05 July 2010 15:32:51 Isaac Gouy escrigué:
> Sturla Molden <sturla <at> molden.no> writes:
> > It is also the kind of tasks where NumPy would help. It would be nice to
> > get NumPy into the shootout. At least for the sake of advertising
> 
> http://shootout.alioth.debian.org/u32/program.php?test=spectralnorm&lang=py
> thon&id=2

Let's join the game :-)  If I run the above version on my desktop computer 
(Intel E8600 Duo @ 3 GHz, DDR2 @ 800 MHz memory) I get:

$ time python -OO spectralnorm-numpy.py 5500
1.274224153

real    0m9.724s
user    0m9.295s
sys     0m0.269s

which should correspond to the 12.86s in shootout (so my machine is around 30% 
faster).  But, if I use ATLAS (3.9.25) so as to accelerate linear algebra:

$ python -OO spectralnorm-numpy.py 5500
1.274224153

real    0m5.862s
user    0m5.566s
sys     0m0.225s

Then, my profile said that building M matrix took a lot of time.  After using 
numexpr to improve this (see attached script), I get:

$ python -OO spectralnorm-numpy-numexpr.py 5500
1.274224153

real    0m3.333s
user    0m3.071s
sys     0m0.163s

Interestingly, memory consumption also dropped from 480 MB to 255 MB.  
Finally, if using Intel's MKL for taking advantage of my 2 cores:

$ python -OO spectralnorm-numpy-numexpr.py 5500
1.274224153

real    0m2.785s
user    0m4.117s
sys     0m0.139s

which is a 3.5x improvement over the initial version.  Also, this seems faster 
(around ~25%), and consumes similar memory than the fastest version written in 
pure C in "interesting alternatives" section:

http://shootout.alioth.debian.org/u32/performance.php?test=spectralnorm#about

I suppose that, provided that Matlab also have a JIT and supports Intel's MKL, 
it could beat this mark too.  Any Matlab user would accept the challenge?

-- 
Francesc Alted
# The Computer Language Benchmarks Game
# http://shootout.alioth.debian.org/
#
# Contributed by Sebastien Loisel
# Fixed by Isaac Gouy
# Sped up by Josh Goldfoot
# Dirtily sped up by Simon Descarpentries
# Sped up with numpy by Kittipong Piyawanno
# More speed-up with Numexpr by Francesc Alted

from sys import argv
import numpy as np
import numexpr as ne

def spectralnorm(n):
    u = np.matrix(np.ones(n))
    j = np.arange(n, dtype=np.float64)
    M = np.empty(n*n, dtype=np.float64).reshape(n,n)
    expr = "1.0 / ((i + j) * (i + j + 1) / 2 + i + 1)"
    for i in xrange(n):
        M[i] = ne.evaluate(expr)
    MT = M.T
    for i in xrange(10):
        v = (u*MT)*M
        u = (v*MT)*M
    print "%0.9f" % ((u*v.T).sum() / (v*v.T).sum())**0.5

spectralnorm(int(argv[1]))
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to