Increase performance with the first-c + + extension python

Source: Internet
Author: User

The previous time wrote two articles about how to improve the efficiency of Python, one is from the perspective of the Python language itself, and the other is from the perspective of the interpreter (using PyPy), interested can look. From another point of view, how to improve the efficiency of python, that is, the use of C + + to extend Python to improve performance. We know that the Python interpreter, which is downloaded from the Python web site, is written in C, so you can also extend it to achieve better execution performance. Python provides API interfaces that we can easily extend, all of which are contained in the Python.h header file, which is introduced when writing C code. Let me start with the experience of extending Python in C/C + +. Affirm all systems Ubuntu 16.04 LTS.

( a) how to compile the. c File under Ubuntu

To tell the truth, the first problem that bothered me with this part of the study was that I didn't know how to compile it under Ubuntu. c files, used to be in the university, with Windows systems, Visual Studio 2010 installed, code writing, compilation is very convenient. Not in Ubuntu under the development of C language, so this part of the knowledge is not quite understood, fortunately, the information on the Internet is very rich, will soon be able to find the corresponding information. In fact, Ubuntu system comes with C language compiler, that is GCC. Don't believe you can open the terminal, enter the name: GCC--version, display the following information:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/88/8E/wKioL1f7phOjMcQfAACDEmo0gas223.png-wh_500x0-wm_3 -wmp_4-s_662419886.png "title=" Selection _034.png "alt=" Wkiol1f7phojmcqfaacdemo0gas223.png-wh_50 "/>

If not, you can also install your own, the installation command is as follows:

sudo apt-get install gcc


It is also recommended to install a build-essentialL package, which is to provide the list information of the compiler must have a package, that is, the compiler has the package it does not know where the header file, only to know where the library function, will also download the dependent software package, Finally, a development environment is formed.

OK, now write a simple. c file.

Open the terminal, enter the command vim hello.c, of course, you can also use Gedit to edit the code, as long as the file extension is not mistaken for the line. The code is as follows:

#include <stdio.h>int main () {int i;for (i=0;i<3;i++) printf ("hello,gcc!\n");}

Then save the file and exit. Compiled into an executable file with the following command:

GCC Hello.c-o Hello

then enter the./hello to execute the file. Effects such as.

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/88/91/wKiom1f7qzSBYrNTAADXPfQEe4E986.png-wh_500x0-wm_3 -wmp_4-s_4149772530.png "title=" Selection _035.png "alt=" Wkiom1f7qzsbyrntaadxpfqee4e986.png-wh_50 "/>

Here is just a brief talk about the use of the Linux gcc, about it more usage can be on the Internet to check the information, do not do too much introduction.


(ii) Extension of Python using C + +

1. First use the interface provided by Python to write a certain function of Chinese. c files, such as determining whether a number is prime. Here the file name is test.c, and the C language is used to wrap it in Python. The code is as follows:

#include <python.h>static pyobject *pr_isprime (Pyobject *self,pyobject *args) {     int n,num;    if (! Pyarg_parsetuple (args, "I", &num))         return NULL;     if (num<1) {        return py_buildvalue ("I", 0 );     }    n=num-1;    while (n>1) {         if (num%n==0)              return py_buildvalue ("I", 0);             n--;    }    return py_buildvalue ("I", 1);} static pymethoddef prmethods[]={    {"IsPrime", Pr_isprime,meth_varargs, "check  if an input numbe is prime or not. "}, &NBSP;&NBSP;&NBSP;{NULL,NULL,0,NULL}};VOID&NBSP;INITPR (void) {     (void)  Py_InitModule ( "PR", prmethods);}

Pyobject is the basis of the Python object mechanism, and all objects have the same content, which is defined in Pyobject.

The above code consists of three parts:

One is the export function, the C module externally exposed interface function pr_isprime, with the self and args two parameters, where the parameter args contains the Python interpreter to pass to the C function of all parameters, usually using pyarg_parsetuple () to obtain these parameters.

The second is to initialize the function so that the Python interpreter can initialize the module, starting with INIT, such as INITX.

The third is a list of methods that are provided to the external Python program using a C module Function name Mapping table Prmethods. It is a pymethoddef struct, where members in turn represent method names, exported functions, parameter passing methods, and method descriptions.

Parameter passing is generally set to Meth_varargs, if you want to pass the keyword argument, you can let it with mrth_keywords or operation, if you do not want to accept any arguments, you can use the Meth_noargs, the struct must be {null,null,0,null } represents the end of an empty record.


As for why this, may be related to the function inside the Python.h, this Part I have not studied carefully, so this article also called the topic with the "first use", if it is clear later, I will "re-use", to share learning experience with you.


2. Writing setup.py scripts

The code is as follows:

Rom distutils.core import setup,extensionmodule = Extension (' PR ', sources=[' test.c ') Setup (name= ' PR test ', Version = ' 1.0 ', Ext_modules=[module])

The Distutils package is used to build and install additional modules in a Python environment. The new module can be either pure python or an extension that is written in C/s + +, or it can be a Python package that contains modules written by C and Python. Do not introduce too much here, have the interest to find information on the Internet to understand.


3. Compile with Python setup.py build, and the system will automatically generate a build subdirectory in the current directory containing a. so file,. o file. Look at the results of my operation.

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/88/8E/wKioL1f7vs-QKPQKAAK9t0faT40011.png-wh_500x0-wm_3 -wmp_4-s_3192941850.png "title=" Selection _036.png "alt=" Wkiol1f7vs-qkpqkaak9t0fat40011.png-wh_50 "/>

After executing the python setup.py build command, the system does generate a build subdirectory in the current directory and has a. so file,. o file.

4. Copy the generated. So file to/usr/local/lib/python2.7/dist-packages, or add the directory path where the. So file is located to the Sys.path, and then you can use the C extension module. Such as:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/88/92/wKiom1f7v_Sh9IkwAACW12BQw1E690.png-wh_500x0-wm_3 -wmp_4-s_2046114597.png "title=" Selection _037.png "alt=" Wkiom1f7v_sh9ikwaacw12bqw1e690.png-wh_50 "/>


(iii) performance under simple tests

The code is as follows:

 #coding =utf-8import primport datetimedef is_prime (n):     if n <= 1:#        return  false    else:        a = n -  1        while (a > 1):             if n % a == 0:                 return True             a = a - 1         return falset1 = datetime.datetime.now () is_prime (8888888) t2  = datetime.datetime.now () pr.isprime (8888888) t3 = datetime.datetime.now () print t3 -  t2,t2 - t1 

where Is_prime () is written in Python to determine whether a positive number is a prime number. Its loop algorithm and the PR module inside the IsPrime () is the same, but the efficiency is very different, look at the results of the operation.

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/88/8E/wKioL1f7xGSSstGeAAAqTcB5foE256.png-wh_500x0-wm_3 -wmp_4-s_1477897753.png "title=" Selection _038.png "alt=" Wkiol1f7xgssstgeaaaqtcb5foe256.png-wh_50 "/>

Obviously the function in the package with the C extension is fast and the block is nearly 20 times times, the larger the value, the greater the gap in speed, the more interested, you can try. Of course, this is just a qualitative analysis.

Increase performance with the first-c + + extension python

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.