开发者

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

i have prepar开发者_StackOverflow中文版ed 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,


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜