Why not a Python compiler?

Stefan Behnel stefan_ml at behnel.de
Thu Feb 7 10:52:57 EST 2008


Jean-Paul Calderone wrote:
> On Thu, 07 Feb 2008 11:03:12 +0100, Stefan Behnel <stefan_ml at behnel.de>
> wrote:
>> Take a look at Cython. It's an optimising Python-to-C compiler for
>> writing
>> Python extensions. So you can basically take a Python module and
>> compile it to
>> C code that runs against the CPython runtime.
>>
>> http://cython.org/
> 
> It's a not-quite-Python-to-C compiler.

Ok, there are differences. For example, you can't define functions dynamically
(it doesn't currently support closures anyway). But it already supports a much
wider subset of the language than Pyrex originally did. For example, you can
use list comprehensions and Python 3 keyword-only arguments in function
signatures. I would expect it would compile quite a lot of Python code out
there without or with only minor modifications.


> I don't think it is an optimizing
> compiler either.  Can you provide a reference for this?

It optimises a lot of common patterns into very fast sequences of Python API
calls (or even generates specialised non-API code for them). It also generates
optimised runtime code for special cases based on the type of an object (e.g.
if the object you iterate turns out to be a list, it uses fast list API calls
in loops, and a standard iterator otherwise). So the generated code is usually
much faster than what Pyrex gives you. Robert and I had an optimise session
lately where we dropped the function call-overhead by some 20-50% (!) compared
to the preceding Cython version (not even to Pyrex), just depending on the
signature.

I think that qualifies for an "optimising" compiler.

Stefan



More information about the Python-list mailing list