[Tutor] Cython question

Alan Gauld alan.gauld at btinternet.com
Sat Jul 2 13:40:00 CEST 2011


"Albert-Jan Roskam" <fomcl at yahoo.com> wrote

> As far as I understood it requires the functions to be re-written in 
> a
> Python-like langauge, 'minus the memory manager'.

Thats what I understand too, but I've only read the web docs :-)

> converted to C and subsequently compiled to a .dll or .so file.
> The original program listens and talks to that .dll file.

I believe it effectively becomes a module that you
import like any other.

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

That depends on the structure of your code.

Profile it first to prove that the functions you are
optimising are in fact the places where the bulk
of the time is spent.

Then check that those functions aren't spending
their time calling C functions already. If all you have
is a loop calling a fubction that is already in C then
cython is unlikely to give huge improvement. But
if you have a lot of python code doing real
processing then yes cython (or similar) should
make a significant difference (ie up to say, 10 times
faster)

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

If you optimise the wrong bits you might just waste
your time and it will make future maintenance
more difficult because you have two environments
to support.  Have you tried optimising the Python
code first?

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

In the same app yes, in the same function I'd guess
probably not. But I'll let others with more experience
of using both comment.

But bear in mind I've only read the cython docs,
not actually used it...

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list