[Tutor] Cython question

Stefan Behnel stefan_ml at behnel.de
Sat Jul 2 13:29:31 CEST 2011


Albert-Jan Roskam, 02.07.2011 11:49:
> Some time ago I finished a sav reader for Spss .sav data files (also with the
> help of some of you!):
> http://code.activestate.com/recipes/577650-python-reader-for-spss-sav-files/
>
> It works fine, but it is not fast with big files. I am thinking of implementing
> two of the functions in cython (getValueChar and getValueNum).
> As far as I understood it requires the functions to be re-written in a
> Python-like langauge

"rewritten" only in the sense that you may want to apply optimisations or 
provide type hints. Cython is Python, but with language extensions that 
allow the compiler to apply static optimisations to your code.


>, 'minus the memory manager'.

Erm, not sure what you mean here. Cython uses the same memory management as 
CPython.


> That little piece of code is
> converted to C and subsequently compiled to a .dll or .so file. The original
> program listens and talks to that .dll file. A couple of questions:
> -is this a correct representation of things?

More or less. Instead of "listens and talks", I'd rather say "uses". What 
you get is just another Python extension module which you can use like any 
other Python module.


> -will the speed improvement be worthwhile? (pros)

Depends. If your code is I/O bound, then likely not. If the above two 
functions are true CPU bottlenecks that do some kind of calculation or data 
transformation, it's likely going to be faster in Cython.


> -are there reasons not to try this? (cons)

If your performance problem is not CPU related, it may not be worth it.


> -is it 'sane' to mix ctypes and cython for nonintensive and intensive
> operations, respectively?

Why would you want to use ctypes if you can use Cython?

Stefan



More information about the Tutor mailing list