欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python C扩展实践&性能对比

程序员文章站 2022-03-21 16:29:30
...

C扩展实践

因为性能等一些原因,希望用C来扩展python。有多种方法,例如:

  • ctypes调用so
  • cython
  • python接口的C函数

这里阐述最后一种方式的实现。

  1. 首先需要 #include <Python.h>
  2. 需要实现下面三个函数:
static PyObject *funcName(PyObject *self, PyObject *args)
    /* 函数定义 */
static PyMethodDef methodsList[]
    /* 方法映射 */
PyMODINIT_FUNC initModule()
    /* Module初始化(python3 Module的定义和初始化略有不同,见下面示例代码) */

完整代码:

#include <Python.h>
#include <math.h>

#define PI acos(-1)

#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif

void initcmath(void); /* Forward */

main(int argc, char **argv)
{
    /* Pass argv[0] to the Python interpreter */
    Py_SetProgramName(argv[0]);

    /* Initialize the Python interpreter.  Required. */
    Py_Initialize();

    /* Add a static module */
    initcmath();

    /* Define sys.argv.  It is up to the application if you
       want this; you can also leave it undefined (since the Python
       code is generally not a main program it has no business
       touching sys.argv...)

       If the third argument is true, sys.path is modified to include
       either the directory containing the script named by argv[0], or
       the current working directory.  This can be risky; if you run
       an application embedding Python in a directory controlled by
       someone else, attackers could put a *-horse module in the
       directory (say, a file named os.py) that your application would
       then import and run.
    */
    PySys_SetArgvEx(argc, argv, 0);

    /* Do some application specific code */
    printf("Hello, brave new world\n\n");

    /* Execute some Python statements (in module __main__) */
    PyRun_SimpleString("import sys\n");
    PyRun_SimpleString("print sys.builtin_module_names\n");
    PyRun_SimpleString("print sys.modules.keys()\n");
    PyRun_SimpleString("print sys.executable\n");
    PyRun_SimpleString("print sys.argv\n");

    /* Note that you can call any public function of the Python
       interpreter here, e.g. call_object(). */

    /* Some more application specific code */
    printf("\nGoodbye, cruel world\n");

    /* Exit, cleaning up the interpreter */
    Py_Exit(0);
    /*NOTREACHED*/
}

int fastfactorial(int n){
 if(n<=1)
 return 1;
 else
 return n * fastfactorial(n-1);
}

double calculatearea(float r){
    double s;
    float r1, r2, r3, r4, r5;

    r1 = cosf(r);
    r2 = sinf(r);
    r3 = log10(r);
    r4 = PI;
    r5 = 666.666;
    r = (((r1 + r2) - r3) * r4)/r5;

    s = PI * pow(r, 2);
    return s;
}

static PyObject* factorial(PyObject* self, PyObject* args){
int n;
if (!PyArg_ParseTuple(args,"i",&n))
  return NULL;
int result = fastfactorial(n);
return Py_BuildValue("i",result);
}

static PyObject* do_calculation(PyObject* self, PyObject* args){
float n;
if (!PyArg_ParseTuple(args,"f",&n))
  return NULL;
double result = calculatearea(n);
return Py_BuildValue("d",result);
}

static PyMethodDef mainMethods[] = {
 {"factorial", factorial, METH_VARARGS, "Calculate the factorial of n"},
 {"do_calculation", do_calculation, METH_VARARGS, "Calculate the area of r"},
 {NULL, NULL, 0, NULL}
};

#ifdef PY3K
// module definition structure for python3
static PyModuleDef cmath = {
 PyModuleDef_HEAD_INIT,
 "cmath","Factorial Calculation",
 -1,
 mainMethods
};

PyMODINIT_FUNC PyInit_cmath(void){
 return PyModule_Create(&cmath);
}
#else
// module initializer for python2
PyMODINIT_FUNC initcmath(void) {
	// PyImport_AddModule("factorial");
    (void) Py_InitModule("cmath", mainMethods);
}
#endif

编译(使用distutils):

from distutils.core import setup, Extension

    factorial_module = Extension('cmath', sources=['cmath.c'])
    setup(name='MathExtension',
          version='1.0',
          description='This is a math package',
          ext_modules=[factorial_module]
          )

性能对比

使用上面完整代码中的calculatearea做运算性能对比,在python中做同样实现:

import cmath
import timeit
import math


def do_calculation(r):
    r1 = math.cos(r)
    r2 = math.sin(r)
    r3 = math.log10(r)
    r4 = math.pi
    r5 = 666.666
    r = (((r1 + r2) - r3) * r4)/r5
    return math.pi * r * r


if __name__ == '__main__':
    print do_calculation(555.555)
    print cmath.do_calculation(555.555)

    # print sys.modules['__main__']
    #
    radius = 666.666
    num = 10000000
    t = timeit.Timer("cmath.do_calculation(%f)" % (radius), "import cmath")
    print "C function", t.timeit(num), "sec"

    t2 = timeit.Timer("sys.modules['__main__'].do_calculation(%f)" % (radius))
    print "Python function", t2.timeit(num), "sec"

执行10000000次对比耗时:

Python C扩展实践&性能对比

OK,C实现的方案相同运算速度远快于python实现,目的达到。