Pyrex and Cython

John Arbash Meinel john at arbash-meinel.com
Fri Jul 2 15:08:59 BST 2010


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

Russel Winder wrote:
> Not really on topic, but there are people on this list with experience
> it would be great if I could tap:
> 
> In various little experiments using Pyrex and Cython -- which amount to
> microbenchmarks -- I am finding that the same tight loop, doubles-based
> algorithm coding in Pyrex is significantly slower than using Cython.
> Cython is relatively close to C whilst Pyrex is significantly slower,
> but a lot faster than Python.
> 
> Does this match people's experiences or do I perhaps need to investigate
> whether I have poor Pyrex code whereas I seem to have reasonable Cython
> code.
> 
> Thanks.
> 

Cython does a *lot* more optimization work. If you look at the raw C
code, it does stuff like unlikely() macros for gcc. It also uses a lot
more helper functions. (So instead of using a generic Python object
interface, it has a test for an optimized interface, and uses it if
possible.)

It also allows you to do stuff like:

 cdef list foo

 foo.append(x)

gets translated to:

  PyList_Append(foo, x)

Whereas with older Pyrex code you have to:

cdef extern from "Python.h":
  int PyList_Append(object, object) except -1

...

  cdef foo
  PyList_Append(foo, x)

(Basically, you have to do the translation yourself.)

Pyrex 0.9.8.5 incorporated at least 'cdef list' from Cython, but I think
it still doesn't have stuff like 'cdef dict' or 'cdef tuple'.


If I was starting a new project, I would use Cython, no doubt. The
primary reason we didn't for bzr is because of availability. Pyrex was
barely available, much less cython (if cython was even around yet).

At present, I think bzr is stuck using Pyrex 0.9.6+ syntax, because that
was released on (Hardy?).

John
=:->

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

iEYEARECAAYFAkwt8voACgkQJdeBCYSNAAMvUACfQ76u/MVogHMFiknvAopG47qT
BBUAoJe0NmgrY2Mpsf6Sm7pCaECh9/u6
=NLud
-----END PGP SIGNATURE-----



More information about the bazaar mailing list