Python C extension Practice & performance comparison

C extended practice

Because of performance and other reasons, I hope to extend python with C. There are many ways, such as:

  • ctypes calls so
  • cython
  • C function of python interface

This paper expounds the realization of the last way.

  1. First, you need to include < python. H >
  2. The following three functions need to be implemented:
static PyObject *funcName(PyObject *self, PyObject *args)
    /* Function definition */
static PyMethodDef methodsList[]
    /* Method mapping */
PyMODINIT_FUNC initModule()
    /* Module Initialization (the definition and initialization of python3 Module are slightly different, as shown in the following example code) */

Full code:

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

#define PI acos(-1)

#define PY3K

void initcmath(void); /* Forward */

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

    /* Initialize the Python interpreter.  Required. */

    /* Add a static module */

    /* 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 Trojan-horse module in the
       directory (say, a file named 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 */

int fastfactorial(int n){
 return 1;
 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"},

#ifdef PY3K
// module definition structure for python3
static PyModuleDef cmath = {
 "cmath","Factorial Calculation",

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

Compile (using distutils):

from distutils.core import setup, Extension

    factorial_module = Extension('cmath', sources=['cmath.c'])
          description='This is a math package',

Performance comparison

Use the calculatearea in the above complete code to compare the operation performance, and do the same in 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 comparisons took:

OK, the same operation speed of C is much faster than that of python.

Tags: Python

Posted on Thu, 07 Nov 2019 08:12:28 -0800 by ReeceSayer