Friday, October 10, 2014

Cython is awesome

After some initial frustration with learning Cython, I found the Scipy 2013 Cython tutorial by Kurt Smith of Enthought, which was a hugely positive surprise. IMO it's by far the best intorduction to Cython (the official Cython tutorials can be a bit tedious...).

So I decided to put my newly learned skills to use in optimizing some stochastic differential equation simulations. To make a long story short, here's the original Python+Numpy version:

...and here's the Cython optimized version:
Previously I had to wait about 30s for 1000000 iterations, and now it takes only about .1 seconds... definitely worth the effort ;) (BTW sorry about the lack of documentation... anyway, the algo has an adaptive time step, since I'm simulating some pretty wildly behaving SDEs).