[Pyrex] Quick 'len()' speedup for Pyrex

Phillip J. Eby pje at telecommunity.com
Thu Apr 22 20:05:56 CEST 2004


If your Pyrex code uses the 'len()' builtin, here's a handy quick fix you 
can pop in at the beginning of your code:

cdef extern from "Python.h":
     int len "PySequence_Length" (object o) except -1

This basically tells Pyrex to treat len() as a C call to its C API 
equivalent, rather than doing a global name + builtins lookup, followed by 
a tuple construction, PyObject_CallObject, and tuple deallocation.

I expect there may be other builtins that are similarly replaceable, making 
your Pyrex code look like idiomatic Python, but compile to "real C" instead 
of Python emulation in C.  For example, 2-argument getattr can probably be 
rendered as:

cdef extern from "Python.h":
     object getattr "PyObject_GetAttr" (object o, object a)









More information about the Pyrex mailing list