A faster Python?, Python compiler, Dylan,...

eric eric at enthought.com
Wed Apr 3 17:11:04 EST 2002


Hey Jan,
>
> for i in xrange(1,a.shape[0]):
>         if a[i,i]>30: a[i,i]+=a[i,i+1]
>         else:          a[i,0]*=2.0
>
> Not a particularly useful example, but one which could be much
> accelerated by translating it (automatically) into C.

For this particular example, weave.inline is a perfect solution.  The C++ code
is pretty readable:

from scipy import *

def foo(a):
    code = """
           for (int i = 0; i < _Na(0);i++) {
               if (a(i,i) > 30) a(i,i) += a(i,i+1);
               else             a(i,0) *= 2.0; }
           """
    weave.inline(code,'a',compiler='gcc',
                 type_converters = weave.converters.blitz)

a = rand(3,3)*60
print 'before:', a
foo(a)
print 'after:', a

--------------------------------------------------

C:\temp>python ex.py
before: [[ 52.34835505  45.81040621  34.39133048]
 [ 59.23025608  20.20882666  42.10668683]
 [ 41.57790542  43.00394297  26.22276664]]
after: [[  98.15876126   45.81040621   34.39133048]
 [ 118.46051216   20.20882666   42.10668683]
 [  83.15581083   43.00394297   26.22276664]]


The place where weave breaks down is when you have a lot of calls within the
loop to Python functions.  This is a common problem, and the only way around it
is to have something that compiles the Python to C or assembly or something.
That is what Psycho does.  weave may also do something similar in the forseeable
future.  Pat Miller is integrating some work similar to Psycho into weave that
converts Python to C on the fly using type inferencing and then uses weave to
build the extension module.  Several of the aims are different than psycho, but
the concepts are similar.

I'm all for a faster Python, but a robust compiler for a dynamic language is
difficult.  I'm hoping Psycho pulls it off.  weave is less ambitious and can
fill at least some of the gaps until it does.

regards,

eric


>
> J.
>
> --
> -------------------------------------------------------------------------
> Jan Kybic <kybic at ieee.org>      Robotvis, INRIA, Sophia-Antipolis, France
>        or <Jan.Kybic at sophia.inria.fr>,tel. work +33 492 38 7589, fax 7845
>                     http://www-sop.inria.fr/robotvis/personnel/Jan.Kybic/
>
>







More information about the Python-list mailing list