tags:

views:

50

answers:

4

i have prepared a project in python language ie a TEXT TO SPEECH synthesizer. Which took a total on 1500 lines of code. But there few parts of code due to which it is taking so much time to run the code, i want to replace that parts of code in C/c++ lang so that it runs faster.

So i want to know how can i run these parts of code in C++ or improve its speed in any other way??

please suggest,

+1  A: 

You can write a Python extension module in C or C++.

mipadi
+4  A: 

You could write them in Cython, it's pretty easy.

Alternatively, you can try using numpy, which is already written in C and may have most of the operations you need.

Radomir Dopieralski
is it that i just need to write the code in cython nd import that module into the original code??
kaki
Yes, but you also need to compile it, see http://docs.cython.org/src/quickstart/build.html
Radomir Dopieralski
+3  A: 

You have a few options:

As Radomir mentioned, Cython might be a good choice: it's essentially a restricted Python with type declarations, automatically translated into C then compiled for execution.

If you want to use pure C, you can write a Python extension module using the Python C API. This is a good way to go if you need to manipulate Python data structures in your C code. Using the Python C API, you write in C, but with full access to the Python types and methods.

Or, you can write a pure C dll, then invoke it with ctypes. This is a good choice if you don't need any access to Python data structures in your C code. With this technique, your C code only deals with C types, and your Python code has to understand how to use ctypes to get at that C data.

Ned Batchelder
A: 

You could also try effect of psyco module to your code's running time (Python 2.6 or earlier) or pypy (python JIT written in subset python). There is also C++ compiler shedskin, Windows is not supported in latest versions though.

Tony Veijalainen