logo       

Quick 'len()' speedup for Pyrex: msg#00046

python.pyrex

Subject: Quick 'len()' speedup for Pyrex

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)


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise