On Mar 11, 2008, at Mar 11:8:11 PM, David MacQuigg wrote:

At 03:12 PM 3/11/2008 -0700, Rob Malouf wrote:

On Mar 11, 2008, at 2:57 PM, David MacQuigg wrote:
I guess what I should conclude is that when performance is
important, don't bother trying to optimize Python.  Go straight to
C, and get 10 or 100X improvement.

That hasn't always been my experience.  I found that using psyco and
numpy (along with careful profiling and optimization), I can often get
essentially the same performance from pure Python that I can get from
mixed Python/C solutions, and with much less trouble.

I agree that the Python/C interface is more trouble than it should be, especially for freshman engineering students and technical professionals who don't have time for messy programming details. What I would like to see is something like a simple "directive" I could put in my Python code to say "The following function is in C", and have Python set up the linkage for me.

One of the things that drew me to Python for Numerical work was Pyrex (and now the Cython project). Here is your code in Pyrex (sans-doc string)

def getpts(double cx0,double cy0,double dx):
    vals = []
    cdef int p,i
    cdef double cx,zx,zy,zx2,zy2
    for p from 0<=p<100:     # 100 points
        cx = cx0 + p * dx        #  moving right    x . . . . . .
        zx = zy = 0.0
        for i from 0<=i<1000:     # 100 points
            zx2 = zx*zx
            zy2 = zy*zy
            if (zx2 + zy2) > 4.0 : break  # escape
            zy = 2*zx*zy + cy0
            zx = zx2 - zy2 + cx

        vals.append(i)

    return vals

This code gets compiled to C, and returns this unit test:

Expected:
    1a) Python speed = 787 points/sec
    1b) C speed      = 125500 points/sec
    2a) Python speed = 823 points/sec
    2b) C      speed = 134300 points/sec
Got:
    1a) Python speed = 1214 points/sec
    1b) C speed      = 125700 points/sec
    2a) Python speed = 1287 points/sec
    2b) C      speed = 134500 points/sec


not too shabby! And for only putting the types of the variable into the existing python code, and a slight modification of the for-loop syntax, the conversion of a slow python function into a fast extension module is trivial. One of the best things here is that for debugging, you can put in arbitrary python code. You take the performance hit, but you don't care during debugging, and you take it out again for production.



                                bb


--
Brian Blais
[EMAIL PROTECTED]
http://web.bryant.edu/~bblais



_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to