[Pyrex] Pyrex idioms and optimizations?

skip at pobox.com skip at pobox.com
Thu Jul 12 04:35:40 CEST 2007


Way back in 2004 in a thread on optimization Greg wrote:

    I'm quite happy to forego the dynamicity of builtins in order to
    optimise this sort of thing. I'm planning to pre-declare some of the
    builtins as being equivalent to certain API calls, e.g. len() would be
    equivalent to PyObject_Size().

Googling for "pyrex optimization" doesn't turn up much.  I'm wondering
what's improved since 2004.  I did come across Paul Prescod's 2004 PyCon
slides on the subject.

Are there some standard idioms that represent the "best" way to recode
certain common Python idioms?  For example, I know that list comprehensions
aren't supported, so I might need to use a loop:

    mylist = []
    for x in anotherlist:
        mylist.append(x.someattr)

That seems like a pretty common sort of thing to do.  Is there a "best" way
to do that in Pyrex?  Ideally, I'd like to avoid the method lookup all the
time.  Something like this?

    mylist = []
    for x in anotherlist:
        PyList_Append(mylist, x.someattr)

I guess I'm still thinking like a Python programmer and not quite like a C
programmer.  I keep thinking if I drop into the C API I might as well just
program in C.  Still, it seems that if I mylist is a local vrbl and I never
rebind it in the loop Pyrex should be able to do the PyList_Append trick for
me under the covers.

Skip



More information about the Pyrex mailing list