kaki
kaki

Reputation: 1053

how to replicate parts of code in python into C to execution faster?

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,

Upvotes: 0

Views: 134

Answers (4)

Tony Veijalainen
Tony Veijalainen

Reputation: 5555

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.

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375484

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.

Upvotes: 3

Radomir Dopieralski
Radomir Dopieralski

Reputation: 2585

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.

Upvotes: 5

mipadi
mipadi

Reputation: 410542

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

Upvotes: 1

Related Questions