Friday, May 2, 2008

Embedding C code in Python

There is a very interesting approach to speed up your Python code.
This is an example code I found in the PyInline document.


import PyInline

m = PyInline.build(code="""
double my_add(double a, double b) {
return a + b;
}
""", language="C")

print m.my_add(4.5, 5.5) # Should print out "10.0"

I don't think I need additional explanation on what this program does. It's quite obvious. Under the hood, it executes a compiler on the fly, creates a Python extension module (.pyd file), imports it immediately so that the script can use.

As far as I know, there are three tools.

PyInline http://pyinline.sourceforge.net/

scipy.weave http://www.scipy.org/

instant http://www.fenics.org/wiki/Instant

scipy.weave is the most famous one which is part of scipy, a numerical package (I looked if NumPy has inherited weave from scipy but couldn't find it). instant is based on swig.
If you are interested in reading the source code to see how they work, I recommend you read PyInline first since there are only three .py files.

3 comments:

mihai said...

very interesting post :) thank you!

what py editor are you using?

Koichi Tamura said...

you are welcome.

Some of the tools may only work on *nix.
These tools are mostly developed first on *nix first and sometimes ported/tested on windows.

I looked at their sites but only instant said it worked on windows. weave, probably because scipy runs on windows. pyinline, I don't know if it works, though there was nothing that depends on windows as far as I glanced the source code.

I use eclipse for large systems but when I don't have to use a debugger I just use text editor.

mihai said...

*nix is OK... i am on win & fedora... anyway i'm still deciding if i'm gonna do py in linux or win...

i have eclipse on both platforms :)