← Back to team overview

larry-discuss team mailing list archive

Fwd: Cython function with python fallback

 

I'm just forwarding this message from the pystatsmodels list. Sorry to
those who are on both lists.

---------- Forwarded message ----------
From: Keith Goodman
Date: Sun, May 30, 2010 at 1:32 PM
Subject: Cython function with python fallback
To: pystatsmodels@xxxxxxxxxxxxxxxx


I added the first cython function to the la package. Since statsmodels
may one day use cython, I figured that I should jot down my experience
(less than a day) for future reference.

Wes noticed that a two-line python function in larry was O(N^2) which
made for terrible performance on binary operations (+, -, *, |, etc)
between two large 1d unaligned larrys. I made the function O(N) by
expanding it to three lines. But why stop there? I then converted the
function to cython (9 lines).

The only remaining problem was how to distribute the la package now
that it contained a C extension. Because I have both a C version and
python version of the function, I decided to try to compile the cython
version at package build time (setup.py) and then fall back to the
python version if something went wrong. Instructions for doing that
are here:

http://stackoverflow.com/questions/2398699/how-to-create-python-module-distribution-to-gracefully-fall-back-to-pure-python-c/2398773#2398773

and the corresponding la changeset is here:

http://bazaar.launchpad.net/~kwgoodman/larry/trunk/revision/283

I'm very interested in finding out if the la packages builds on
windows. If the C compilation was successful then the bottom of the
doc string of listmap will say: Note: This is the C version of the
function.

To import listmap:

$ from la.util.misc import listmap