Several ways Python interacts with C + +

Source: Internet
Author: User

Python as a scripting language, the advantage is simple syntax, a lot of things have been encapsulated, directly to use the line, so to achieve the same function, with Python writing is much less than the amount of C/s code. But the pros are bound to be accompanied by shortcomings (that's for sure, or other languages to do), Python is the most criticized a place may be the speed of its operation. This is a common problem for most scripting languages, because there is no compile process and executes directly on a line, so it's a big one. Therefore, in some of the high-speed requirements of the occasion, it is generally used in the compiler-C + + language to write. But most of the time, we want to use Python's profile beautifully, and do not want to lose too much performance, this time there is no way to combine Python and C/C + +? In this way, the performance and speed requirements are not high, you can write with Pyhton, and the key part of the operation is written in C + +, so it is very good. Python is a very common requirement when it comes to scientific calculations or data analysis. To achieve this, Python provides us with more than one workaround. Here I will give you a brief introduction.

One, Cython mixed Python and C

Official website: http://docs.cython.org/en/latest/src/quickstart/overview.html. First look at the official introduction of Cython.

[Cython] is a programming language this makes writing C extensions for the Python language as easy as Python itself. It aims to become a superset of the [python]language which gives it high-level, object-oriented, functional, and dynamic Programming. Its main feature on top of these are support for optional static type declarations as part of the language. The source code gets translated into optimized C + + code and compiled as Python extension modules. This allows to both very fast program execution and tight integration with external C libraries, while keeping up the HIG H Programmer productivity for which the Python language are well known.

In short, Cython is a python with a built-in C data type that is a Python superset that is compatible with almost all of the pure Python code, but can use the C data type. This makes it possible to use the C library at the same time without losing the elegance of Python.

Well, don't talk too much nonsense, just see Cython How to use it. Most of the introduction here from the official website, because Cython involves more, so here is just a simple introduction, detailed information please visit the English official website.

There are two ways to use Cython: The first is to build a python extension file (something like a DLL, a dynamic-link library) that you can import directly. The second is to use the Jupyter notebook or sage notebook inline Cython code.

Look first. Let's just cite the classic Hello World example. Create a new Hello.pyx file and define a Hello function as follows:

def hello (name):    print ("Hello%s."% name)

Then, let's write a setup.py file (write Python extensions almost all to write the setup.py file, and I've also briefly described how to write it before) as follows:

1 #!/usr/bin/env Python 2 #-*-coding:utf-8-*-3 # @Time   : 2017/5/8 9:09 4 # @Author: Lyrichu 5 # @Email  : [em AIL protected] 6 # @File   : setup.py 7 "8 @Description: setup.py for Hello.pyx 9" "from Cython.build import Cyth Onize11 from Distutils.core import setup12 13 # Write the setup function in the setup (     name = "Hello", and     ext_modules = Cythonize ("He Llo.pyx ") 17)

Where Ext_modules writes the name of the. pyx file you want to compile. OK, all the work is done. Next, go to cmd, switch to the file where the setup.py is located, and execute the command: Python setup.py build_ext--inplace compiles a build folder as well as a. pyd file, this PYD file is a Python dynamic extension library,--inplace means to generate a. pyd file in the current file directory, which is generated in the build folder without this sentence. As follows:

Figure 1

As you can see, in addition to generating a PYD file, a. c file is generated. test.py is the file that we use to test, in which we write the following content:

From Hello Import Hellohello ("lyric")

Import the Hello function from the Hello module and call it directly. The result output is Hello lyric.

Then see how to use Cython in Jupyter notebook. If you have a Ipython, an upgraded Python interactive environment, you should have heard Ipyhton notebook's name, and now it's upgraded, renamed Jupyter Notebook. Simply put, this is a Web environment in the interactive use of Python tools, not only can see the results in real-time, but also directly display tables, pictures, etc., the function is very powerful. First you have to install Jupyter notebook. I think I should have brought jupyter after I installed the Ipython. If not, you can directly pip install Jupyter. Then enter the command Jupyter notebook will open jupyter in the browser. As shown in 2:

Figure 2

Click the New button in the upper right corner, you can choose to create a new text file or folder, markdown or Python file, here we choose to create a new Pyhton file, and then we will go to a new window, such as 3:

Figure 3

In[]: Like Ipython, it represents the place where we want to enter the code, after entering the code, click the right triangle symbol, will execute the code.

First input%load_ext Cython, and then execute,% of the beginning of the statement is the Jupyter Magic Command,% is the line command, percent is the unit command, specifically said, there is time to give you a special introduction to the use of notebook.

Next Enter:

1%%cython2 cdef int a = $4 in range (a     ) + = i5 print (a)

%%cython indicates that the Cython embedded in jupyter,cdef is a keyword of Cython, which defines the C type, where a is defined as the int type in C and initialized to 0.

Then the back loop is the sum of 0 to 9 meaning, the last output 45.

In addition, if we want to analyze the execution of the code, we can enter the%%cython--annotate command, so that we can output the results, but also output detailed code performance report. 4 is shown below:

Figure 4

Jupyter notebook can be embedded cython, without our handwriting setup.py files, eliminating the process of compiling, convenient for the use of Cython, so it is not a formal project, just write a small thing with jupyter+cython or very convenient.

The cdef mentioned above, and a slightly more complicated example. Or to cite an example of the official website, write a function that calculates the integral. Create a new Integrate.pyx file and write the following:

#!/usr/bin/env python#-*-coding:utf-8-*-# @Time   : 2017/5/8 9:26# @Author: lyrichu# @Email  : [Email protected] # @File   : integrate.py "@Description: integral operation, using Cython cdef keyword ' def f (double x):    return x**2-xdef Integrate_f (d ouble a,double b,int N):    cdef int i    cdef double s,dx    s = 0    dx = (b-a)/n for    I in range (N):        s + = f ( A + i*dx) *dx return    s # returns definite integral

This code should also be better understood, the F () function is the integrand, a, B is the upper and lower bounds of the integral, n is the number of split small rectangles, note here the variable I,S,DX all are declared with Cdef C type, in general, in need of dense calculation of places such as loops or complex operations, The corresponding variable can be declared as C type, which can speed up the operation.

And then write the same as above setup.py, is to change the Pyx file name, code I will not post. Then Python setup.py build_ext--inplace executes. Get the PYD file and write the test file test.py as follows:

 1 #!/usr/bin/env Python 2 #-*-coding:utf-8-*-3 # @Time: 2017/5/8 9:35 4 # @Author: Lyrichu 5 # @Email: [E Mail protected] 6 # @File: test.py 7 "8 @Description: Test using Cython mixed C with Python integrate function with pure Python write integrate function speed On the difference 9 "" from integrate import integrate_f11 import Time12 a = 1 # integral interval lower than B = 2 # integral interval upper bound N = 10000 # dividing interval number 16 1     7 # Integrate function written with pure Python code def py_f (x): Return x**2-x20 def py_integrate_f (a,b,n): dx = (b-a)/n23 s = 024 for I in Range (N): + = Py_f (A + i*dx) *dx26 return s27-start_time1 = Time.time () integrate _f_res = Integrate_f (a,b,n) print ("Integrate_f_res =%s"% integrate_f_res) end_time1 = Time.time () Print (U "Cython version) This calculation takes time:%.8f "% (end_time1-start_time1)) start_time2 = Time.time () py_integrate_f_res = Py_integrate_f (A,b,N)-pri NT ("Py_integrate_f_res =%s"% py_integrate_f_res) PNs end_time2 = Time.time () print (U "python version calculation time:%.8f"% (End_time2- start_time2)) 

The above code, we re-use Python to write an integral function py_integrate_f, and PYD in the Integrate_f function to perform the comparison, the results are as follows (Figure 5):

Figure 5

As you can see, the version that uses Cython is about 4, 5 times times faster than a pure Python version, and this is just the result of changing a few variables to C type, so Cython can actually easily mix python with C to get a speed boost, Without losing the simplicity and elegance of python.

Finally, the next Cython how to call C libraries. C Language Stdlib Library has a atoi function, you can convert the string to an integer, the math library has a sin function, we take these two functions as an example. Create a new Calling_c.pyx file with the following file contents:

From Libc.stdlib cimport atoifrom libc.math cimport sindef parse_char_to_int (char * s):    assert S was not NULL, "byte str ing value is NULL "    return Atoi (s) def f_sin_squared (Double x):    return sin (x*x)

The first two lines import the functions in the C language, and then we customize two functions, Parse_char_to_int can convert the string to an integer, and f_sin_squared calculates the value of the sin function with x squared. Write the setup.py file, as before, but note that under the UNIX system, the math library is not linked by default, so you need to indicate its location, then under the UNIX system, the contents of the setup.py file need to add extension, as follows:

From Distutils.core import setupfrom distutils.extension import extensionfrom cython.build import cythonizeext_modules= [    Extension ("Calling_c",              sources=["Calling_c.pyx"],              libraries=["M"] # Unix-like specific    )]setup (  name = "Calling_c",  ext_modules = Cythonize (ext_modules))

Then you can edit it directly. The test.py file is as follows:

1 #!/usr/bin/env Python 2 #-*-coding:utf-8-*-3 # @Time   : 2017/5/8 12:21 4 # @Author: Lyrichu 5 # @Email  : [E Mail protected] 6 # @File   : test.py 7 "8 @Description: Test File 9" "from Calling_c import F_sin_squared,parse_  char_to_int11 str = "012" str_b = bytes (str,encoding= ' utf-8 ') n = parse_char_to_int (str_b) print ("n =%d"%n) from Math import pi,sqrt16 x = sqrt (PI/2) + res = f_sin_squared (x) Print ("Sin (pi/2) =%f"% res)

It is important to note that the Python string cannot be passed directly to the Parse_char_to_int function and needs to be converted to the bytes type and then passed in. The result of the operation is:

n = 12
Sin (PI/2) =1.000000

If you do not want to import the C language module through LIBC, Cython also allows us to declare a C function prototype to import, an example is as follows:

# Self Declaration C function prototype cdef extern from "math.h":    cpdef Double cos (double x) def f_cos (Double x):    return cos (x)

The extern keyword is used.

Write the setup.py file every time, then compile it, it's a little cumbersome. Cython also provides an easier way to do this: Pyximport. by importing Pyximport (which is installed automatically when installing Cython), it is straightforward and convenient to call functions directly in PYX without introducing additional C libraries. With the preceding Hello module as an example, after writing the hello.py file, write a pyximport_test.py file with the following file contents:

Import Pyximportpyximport.install () Import Hellohello.hello ("lyric")

Running directly will find that the Hello module can be imported correctly.

For more information on Cython, please visit our website for your own review.

Other ways to mix Python and C + + programming are to use CTYPES,CFFI modules and swig. Originally want to write together, think or separate write it, otherwise too long. Follow-up will be updated continuously, please pay attention.

Several ways Python interacts with C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.