[Pyrex] Optimisation in Cython and Pyrex

Stefan Behnel stefan_ml at behnel.de
Fri Feb 26 10:45:36 CET 2010


Greg Ewing, 26.02.2010 00:29:
> Stefan Behnel wrote:
>> As a general rule of thumb, the code generated by Cython is quite a bit
>> longer and substantially faster than the corresponding Pyrex code.
> 
> Be careful you're not comparing apples with oranges here,
> though. Pyrex is capable of generating quite efficient code
> for many things, it's just not as automatic as Cython.

I know that it's capable of doing that, but it certainly involves a lot
more work and manual tuning. And there really are a few areas where you
cannot beat Cython's internal optimisations by hand-tuning user code at the
language level.

The Cython project has implemented tons of optimisations in all sorts of
places. An important one is that argument unpacking uses heavily tuned and
thoroughly benchmarked code that matches exactly the signature of the
function/method and even takes common call patterns into account. So the
Python call overhead is generally lower with Cython. There's nothing you
can do about that in user code.

You can cpdef Python functions in your module so that internal calls run at
C speed. You can do that in Pyrex by splitting the function into a C
function and a Python wrapper. But it's just two characters in Cython code.

Cython code can rely on a whole battery of micro optimisations for builtin
types and functions, including type conversions and item access. Most of
them gain from optimistic fast paths at the C level, as you guessed.

We even apply some macro-level parse tree transformations that replace
certain code patterns with equivalent but faster code. That includes some
iterators (e.g. enumerate) and several types of looping constructs and
conditions (e.g. a C switch statement for integer based if-elif-else
chains), as well as certain instantiation patterns for builtin types, such
as "dict([list-comp])", which is transformed into a fast dict
comprehension. Just take a glance at the Optimize.py source module:

http://hg.cython.org/cython-devel/file/tip/Cython/Compiler/Optimize.py#l47

I mean the sum of all of that when I say that, in general, Cython generated
code is substantially faster than the code that Pyrex generates. And I
would even add that, in some cases, this also applies to hand-tuned code.

Stefan



More information about the Pyrex mailing list