Faqts : Computers : Programming : Languages : Python : Modules

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 6 people (100%) answered Yes
Recently 6 of 6 people (100%) answered Yes

Entry

How can I mix C code with python?
How do I make the core loop of my program run faster?

Jun 3rd, 2002 12:58
Michael Chermside,


There are several ways to combine C/C++ with Python. Most claim that
this combination gives the advantages of Python's flexibility and ease
of development combined with C's close-to-the-machine speed.
However, be warned: nearly all practicioners say you should only try to
optimize AFTER you have written the program, and then ONLY if it isn't
"fast enough" for your purposes. Premature optimization is the root of
many unnecessary headaches. And then you should PROFILE, and optimize
only the hotspots.
That being said, here are a few pointers:
PyInline (http://pyinline.sourceforge.net/) is a module which will allow
you to write methods inline in C.
Boost Python (http://www.boost.org/libs/python/doc/) is a library for
wrapping C++ libraries that already exist to make them accessible from
Python.
Swig (http://www.swig.org/) is another library for connecting C and C++
code with Python.
And, of course, Python itself is written in C, and it is designed to be
(somewhat) easy to write "extension modules" to invoke C/C++ code from
Python. The extending/embeding manual
(http://www.python.org/doc/current/ext/ext.html) is the most
authoritative resource.