Cython instead of Pyrex?

John Arbash Meinel john at arbash-meinel.com
Sat Feb 7 21:47:01 GMT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

dhruva wrote:
> Hello,
>  Not that I am an expert in this field, I was looking for Pyrex and related tools and came across Cython which claims to be faster than Pyrex. Has it been tried in bzr? Or, is it totally irrelevant?
> 
> -dhruva
> 

I believe there are a few issues.

1) Pyrex has been around longer, so it is more likely to be available.
(Last I checked, cython wasn't packaged for ubuntu, for example.)

2) Cython *does* help in certain areas, though often they are things
that you can work around. For example, Cython will recognize that a
variable is a list, and then compile in the direct PyList_Append()
function, rather than using the more-generic object interface. (Which
involves PyObject_Getattr(obj, 'append') and then building up a tuple,
and a bunch of other overhead.)
However, if it is performance critical code, you can just use
PyList_Append yourself, which is compatible with both cython and pyrex,
and even old versions of pyrex.

3) At least some of Cython's optimizations have been backported into
Pyrex. I'm not sure which ones, but I noticed that with newer versions
of pyrex, len(foo) is translated differently. In old versions it uses a
getattr lookup in the builtins dictionary, and in later ones it
translates the function to PyObject_Length().

Just to say that *some* of what Cython did to make itself faster has
already been brought back into Pyrex.

4) Cython supports list comprehensions, which would be nice to have, but
you can always write a for loop. For loops with an integer counter
(rather than an object) are just as fast. It is just that you can't
copy&paste the python code.

Anyway, we talked about it a while back, and basically (1) was the big
deal-breaker. That we were more likely to have a new version of Pyrex
available than Cython. That doesn't mean we are stuck going that way,
but to date, we've done all the inlining to make code work fast with
Pyrex, so we don't gain much by using cython.

John
=:->

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkmOAVQACgkQJdeBCYSNAAOZyQCfeKMOHmUtCafJqceRkuv0n3Kp
BXEAoIAvdXeBtcy1E5CzFjQxm+lunRTb
=94nh
-----END PGP SIGNATURE-----



More information about the bazaar mailing list