Summary of several methods for interaction between Python and C/C ++, and several python Methods

Source: Internet
Author: User
Tags jupyter jupyter notebook

Summary of several methods for interaction between Python and C/C ++, and several python Methods

Preface

As a scripting language, python has a simple syntax. Many things have been encapsulated and can be used directly. Therefore, the same function is implemented, writing in Python is much less than using C/C ++ code. However, the advantages must also be accompanied by disadvantages (this is positive, or other languages are required). One of the most criticized aspects of python is its running speed. This is a common problem faced by most scripting languages. Because there is no compilation process, the code is directly executed one by one, so it takes a long time. Therefore, C/C ++ compilation languages are generally used for high speed requirements. However, many times, we want to use the elegant introduction of python without losing too much performance. Is there a way to combine python with C/C ++? In this way, pyhton can be used for writing where the performance and speed requirements are not high, while C/C ++ can be used for key operations. This is great. Python is a very common requirement for scientific computing or data analysis. To implement this function, python provides more than one solution.

Next I will introduce them one by one.

I. Cython mixed python and C

Official Website: http://docs.cython.org/en/latest/src/quickstart/overview.html. First, let's take a look at cython's official introduction.

[Cython] is a programming language that 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 is support for optional static type declarations as part of the language. the source code gets translated into optimized C/C ++ code and compiled as Python extension modules. this allows for both very fast program execution and tight integration with external C libraries, while keeping up the high programmer producties for which the Python language is well known.

In short, cython is a python with built-in c data type. It is a python superset that is compatible with almost all pure python code, but can use c data type. In this way, you can use the c library without losing the elegance of python.

Well, don't talk too much nonsense. Let's take a look at how to use cython. Most of the introductions here are from the official website. Because cython involves a lot of things, this is just a brief introduction. For more information, please go to the official website.

Cython can be used in two ways: compile and generate Python extension files (a dll-like Dynamic Link Library), which can be directly imported. The second is to use jupyter notebook or sage notebook to inline cython code.

First, first. Let's take the most classic hello world example. Create a new hello. pyx file and define a hello function as follows:

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

Next, let's write a setup. py file (almost all python extensions must be written in the setup. py file. I have briefly introduced how to write it before) as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-# @ Time: # @ Author: Lyrichu # @ Email: 919987476@qq.com # @ File: setup. py ''' @ Description: setup. py for hello. pyx ''' from Cython. build import cythonizefrom distutils. core import setup # compile the setup function setup (name = "Hello", ext_modules = cythonize ("hello. pyx "))

The name of the. pyx file to be compiled is written in ext_modules. OK. All work is finished. Next, go to cmd, switch to the file where setup. py is located, and run the following command:python setup.py build_ext --inplace A build folder and. pyd file. This pyd file is the dynamic extension library of python, which is generated under the current file directory. the pyd file is generated in the build folder without adding this sentence.

As follows:

Figure 1

It can be seen that in addition to generating a pyd file, a. c file is also generated. Test. py is the file we use for testing. The following content is written in it:

from hello import hellohello("lyric")

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

Let's take a look at how to use cython in jupyter notebook. If you have installed ipython, an upgraded python interactive environment, you should have heard of ipyhton notebook. Now it has been upgraded and renamed jupyter notebook. Simply put, This is a tool that allows you to use python interactively in a webpage environment. It not only allows you to view computing results in real time, but also allows you to directly display tables, images, and other features. First, you have to install jupyter notebook. I remember that after ipython is installed, jupyter should be installed. If not, you can directlypip install jupyter. Then enter the commandjupyter notebook Jupyter will be opened in the browser.

See Figure 2:

Figure 2

Click the new button in the upper right corner to create a new text file or folder, markdown or python file. Here we choose to create a new pyhton file and then go to a new window, example 3:

Figure 3

In []: Like ipython, it indicates where we want to enter the code. after entering the code, click the triangle symbol to the right to execute the code.

Enter%load_ext cython Run the command. The statement starting with "%" is the magic command of jupyter, "%" is the line command, and "%" is the unit command. If you have time, I will introduce the usage of notebook.

Next, enter:

 %%cython cdef int a = 0 for i in range(10):  a += i print(a)

% Cython indicates that cython is embedded into jupyter. cdef is the keyword of cython and is used to define the c type. Here, a is defined as the int type in c and initialized to 0.

Then the subsequent loop is to accumulate the meaning of 0 to 9, and finally output 45.

If you want to analyze the code execution, enter%%cython --annotate In this way, you can output the results and the detailed code execution report.

4:

Figure 4

Jupyter notebook can be embedded with cython. We don't need to write setup manually. the py file saves the compilation process and facilitates the use of cython. Therefore, it is very convenient to use jupyter + cython to write a small object instead of a project.

Cdef is mentioned above. Let's look at another example of a slightly complex point. Refer to the example on the official website to write a function for calculating points. Create the integrate. pyx file and write the following content:

#! /Usr/bin/env python #-*-coding: UTF-8-*-# @ Time: # @ Author: Lyrichu # @ Email: 919987476@qq.com # @ File: integrate. py ''' @ Description: integral operation. Use the cython cdef keyword ''' def f (double x): return x ** 2-xdef integrate_f (double 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 a fixed point.

This code is also easy to understand, f()The function is the product function, a and B are the upper and lower limits of the integral, and N are the number of small rectangles. Note that all the variables I, s, and dx are declared as c type with cdef, generally, when intensive computing is required, such as loops or complex operations, the corresponding variable can be declared as the c type, which can speed up the operation.

Then write setup. py like above, that is, change the file name of pyx and I will not post the code. Thenpython setup.py build_ext --inplace Run. Obtain the pyd file and write the test. py file as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-# @ Time: # @ Author: Lyrichu # @ Email: 919987476@qq.com # @ File: test. py ''' @ Description: test the speed difference between the cython and python integrated functions and the integrated functions written in pure python ''from integrate import integrate_fimport timea = 1 # lower bound of the integral interval B = 2 # Integral upper Limit N = 10000 # Number of intervals # Use the integrate function def py_f (x) Written in pure python code): return x ** 2-xdef py_integrate_f (a, B, N): dx = (B-a)/N s = 0 for I in range (N ): s + = py_f (a + I * dx) * dx return sstart_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 calculation time: %. 8f "% (end_time1-start_time1) start_time2 = time. time () py_integrate_f_res = py_integrate_f (a, B, N) print ("py_integrate_f_res = % s" % py_integrate_f_res) end_time2 = time. time () print (u "python version computing time: %. 8f "% (end_time2-start_time2 ))

In the above Code, we re-use python to write an integral function py_integrate_f, Which is compared with the integrate_f function in pyd. The result is as follows (figure 5 ):

Figure 5

It can be seen that the version of cython is about 4 or 5 times faster than that of pure Python, and this is only the result of changing several variables to the c type, cython can easily mix python and c to improve the speed without losing the simplicity and beauty of Python.

Finally, let's talk about how cython calls the c libraries. C language stdlib library to have a atoi function that converts strings into integers. The math Library has a sin function. Let's take these two functions as an example. Create a calling_c.pyx file with the following content:

from libc.stdlib cimport atoifrom libc.math cimport sindef parse_char_to_int(char * s): assert s is not NULL,"byte string value is NULL" return atoi(s)def f_sin_squared(double x): return sin(x*x)

The first two rows have imported functions in C language, and we have customized two functions. parse_char_to_int can convert the string to an integer. f_sin_squared calculates the sin function value of x square. Write setup. the py file is similar to the previous one, but note that in unix systems, the math library is not linked by default, so you need to specify its location. In unix systems, setup. you need to add an Extension entry to the content of the py file 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 directly compile it. The test. py file is as follows:

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2017/5/8 12:21# @Author : Lyrichu# @Email : 919987476@qq.com# @File : test.py'''@Description: test file'''from calling_c import f_sin_squared,parse_char_to_intstr = "012"str_b = bytes(str,encoding='utf-8')n = parse_char_to_int(str_b)print("n = %d" %n)from math import pi,sqrtx = sqrt(pi/2)res = f_sin_squared(x)print("sin(pi/2)=%f" % res)

Note that Python strings cannot be passed in directly.parse_char_to_int Function, which must be converted to the bytes type before being passed in. The running result is:

n = 12sin(pi/2)=1.000000

If you do not want to use libc to import the c language module, cython also allows us to declare the c function prototype for import. An example is as follows:

# Self-declared 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.

Every time you write the setup. py file and then compile it, it is a little troublesome. Cython also provides a simpler method: pyximport. By importing pyximport (which is automatically installed when cython is installed), you can directly call functions in pyx without introducing an additional c library, which is more direct and convenient. The preceding hello module is used as an example. After compiling the hello. py file, compile a pyximport_test.py file with the following content:

import pyximportpyximport.install()import hellohello.hello("lyric")

Run the command directly and you will find that the hello module can be correctly imported.

For more information about cython, visit the official website.

Other python and c/c ++ mixed programming methods include ctypes, cffi module, and swig. If you want to write them together, think about writing them separately. Otherwise it will be too long. It will be updated in the future. Stay tuned.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.