Saturday, April 14, 2012

More Cython

I've made substantial progress in getting the code to work with Cython, but I'm still working through all the little errors that crop up with I try to optimize the code for it.

I'm going to write something here about Cython, a language I am not yet completely proficient in, so please take the following with that caveat in mind:

The idea behind Cython is that it's basically a C translator for Python.  It takes source files written in Python but with the extension pyx and, using another Python script that functions a bit like a Makefile, first converts them into C source files (with a .c extension) then converts those into Python compiled modules (.pyd) that a Python interpreter can then import.

If you just rename your Python files to .pyx and write a plain setup.py to convert them, then they should work as-is.  In my case they do, and the result is an about 10-20% speedup, not bad but nothing to write home about.

But one thing you can do with Cython is add keywords to variables, functions and classes that impart type information to them, which supposedly helps tremendously with heavy processing-focused applications, applications such as cellular automation.  You can also define a class so that, instead of using a Python dictionary to hold attributes, you use a C struct, by using the cdef keyword when defining the class.  These things are basically what I've spent the past few days of work on In Profundis doing.

Unfortunately, accessing a cdef class's attributes from outside the class appears to work weirdly.  Sometimes it seems to work, but sometimes it throws up a runtime error that it can't find the attribute.  For it to work consistently, apparently, I have to write accessor functions for those attributes.  And sometimes (not all the time) even those accessor functions aren't found.  (I think it might have to do with the return type of the function, which I might have to declare.  I should look into that.)

Anyway, just wanted to keep you all posted.  In Profundis work has been faster this past week than it's been for a long time.  It's having to share time with a for-pay game project I'm working on (which also has to do with celular automation), but it's continuing fairly well.  Expect more updates soon.

No comments:

Post a Comment