Use Python An extension of API Write some simple C Extension module , The computing performance and C/C++ Of the same rank .
Test environment :
The header file name is sample.h
, The details are as follows
/* sample.h */
#include <math.h>
extern int gcd(int, int);
extern int in_mandel(double x0, double y0, int n);
extern int divide(int a, int b, int *remainder);
extern double avg(double *a, int n);
typedef struct Point {
double x,y;
} Point;
extern double distance(Point *p1, Point *p2);
C Language program sample.c
, It reads as follows
/* sample.c */
#include <math.h>
/* Compute the greatest common divisor */
int gcd(int x, int y) {
int g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
/* Test if (x0,y0) is in the Mandelbrot set or not */
int in_mandel(double x0, double y0, int n) {
double x=0,y=0,xtemp;
while (n > 0) {
xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
n -= 1;
if (x*x + y*y > 4) return 0;
}
return 1;
}
/* Divide two numbers */
int divide(int a, int b, int *remainder) {
int quot = a / b;
*remainder = a % b;
return quot;
}
Put the standard C Files are compiled into standard dynamic libraries , namely libsamplpe.so
gcc -shared -fPIC sample.c -o libsample.so
# This operation makes no sense , Just to prove C The language file is correct
pysample.c
Be responsible for C Language and Python Language interaction between , Code responsible for numerical conversion in two environments .
In a way pysample.c
More like an interface , Just like header files for standards C file .pysample.c
The contents of the document are as follows :
#include "Python.h"
#include "sample.h"
/* int gcd(int, int) */
static PyObject *py_gcd(PyObject *self, PyObject *args) {
int x, y, result;
if (!PyArg_ParseTuple(args,"ii", &x, &y)) {
return NULL;
}
result = gcd(x,y);
return Py_BuildValue("i", result);
}
/* int in_mandel(double, double, int) */
static PyObject *py_in_mandel(PyObject *self, PyObject *args) {
double x0, y0;
int n;
int result;
if (!PyArg_ParseTuple(args, "ddi", &x0, &y0, &n)) {
return NULL;
}
result = in_mandel(x0,y0,n);
return Py_BuildValue("i", result);
}
/* int divide(int, int, int *) */
static PyObject *py_divide(PyObject *self, PyObject *args) {
int a, b, quotient, remainder;
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
return NULL;
}
quotient = divide(a,b, &remainder);
return Py_BuildValue("(ii)", quotient, remainder);
}
/* Module method table */
static PyMethodDef SampleMethods[] = {
{
"gcd", py_gcd, METH_VARARGS, "Greatest common divisor"},
{
"in_mandel", py_in_mandel, METH_VARARGS, "Mandelbrot test"},
{
"divide", py_divide, METH_VARARGS, "Integer division"},
{
NULL, NULL, 0, NULL}
};
/* Module structure */
static struct PyModuleDef samplemodule = {
PyModuleDef_HEAD_INIT,
"sample", /* name of module */
"A sample module", /* Doc string (may be NULL) */
-1, /* Size of per-interpreter state or -1 */
SampleMethods /* Method table */
};
/* Module initialization function */
PyMODINIT_FUNC
PyInit_sample(void) {
return PyModule_Create(&samplemodule);
}
setup.py
The contents of the document are as follows
# setup.py
from distutils.core import setup, Extension
setup(name='sample',
ext_modules=[
Extension('sample',
['sample.c', 'pysample.c'],
include_dirs = ['sample'],
)
]
)
Finish editing setup.py After the document , Enter the command
python3 setup.py build_ext --inplace
View the compiled results
After compiling, it will generate sample.cpython-36m-x86_64-linux-gnu.so Dynamic link library , That is, the extension module
Edit test file example.py
, The contents are as follows
import sample
print(sample.gcd(35,42))
print(sample.in_mandel(0,0,500))
print(sample.in_mandel(2.0,1.0,500))
print(sample.divide(42,8))
function
python3 example.py
Running results
[[email protected] m2]# python3 example.py
7
1
0
(5, 2)
python setup.py install
python setup.py install
There are two steps :
python setup.py build
python setup.py install
These two steps can be performed separately , You can also just execute python setup.py install
, because python setup.py install
Always first build
after install
.
python setup.py build
yes python
compile module
The process of , Finally, it will generate build
Folder .build
After that install
The process is copying build/lib
File to user specified lib library .
reference :
Python Reverse cracking of fon
This system was created by mys
Project full text index 『Pytho