[PYTHON] Implement part of the process in C ++

Scientific computing libraries such as NumPy and SciPy are not only feature-rich, but also interpreters. It runs so fast that you can't think of it as a language. That should be the case, and as you can see from the source code, the internal implementation makes use of dead features written in compiled languages such as C and Fortran.

Of the calculations that are called repeatedly in Python or Ruby following these, it will be more efficient to describe the general-purpose ones as extensions using a compilation language such as C / C ++. The advantages and disadvantages are as follows.

Even if you use the interpreter language later as part of prototyping before implementing it in C / C ++, you may be able to reuse it by writing the library part in C / C ++.

Write a Python extension library in C ++

Read the official documentation for a detailed explanation.

Python extensions with C and C ++ http://docs.python.jp/3.3/extending/extending.html

The Python API can be incorporated into the C world by embedding "Python.h". These must be called before the C / C ++ Standard Library.

argument

static PyObject *
mymethod(PyObject *self, PyObject *args) {
  const char *text;
  if (!PyArg_ParseTuple(args, "s", &text))
    return NULL;
  ...
}

Functions always have two arguments and are customarily treated as self and args.

Method table

The method table by PyMethodDef defines how to access an object and call a method from Python.

static PyMethodDef SomeMethods[] = {
    ...
    {"module_name",  module_name, METH_VARARGS,
     "Write a description here"},
    ...
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

Library registration

The code that initializes when the library is first called is PyMODINIT_FUNC. Make sure PyModule_Create () is returned.

PyMODINIT_FUNC
PyInit_module_name(void) {
    return PyModule_Create(&module_name);
}

Build extension module

Build with distutils is recommended.

from distutils.core import setup, Extension
module = Extension('module_name', ['mymodule.cpp'])
setup(name='module_name',
      version='1.0',
      ext_modules=[module],
     )

The rest can be used by using setup.py.

python setup.py build
python setup.py install #When installing on the system

Once installed, you will be able to import at any time. If not, you can import by specifying the path of the .so file.

import module_name
module_name.some_method('args')

Use C ++

For the official C ++ standard, Read the standard is recommended. Alternatively, you can refer to C ++ reference book is available.

Use Boost

Boost includes members of the C ++ Standardization Committee With a free library by hand, you can do modern programming using templates. The following example uses boost :: split to split a string.

#include <boost/algorithm/string.hpp> //Use Boost
#include <boost/foreach.hpp>
#include <string>
#include <list>
#include <iostream>

using namespace std; // std::Bring to the namespace
int main ()
{
    string str ("192.168.0.1 192.168.0.2");
    list<string> list_string;
    boost::split(list_string, str, boost::is_space()); //Split the string with spaces

    BOOST_FOREACH(string s, list_string) {
        cout << s << endl;
    }
    return 0;
}

Use C ++ 11

At the time of writing, a minor update to C ++ 11 C ++ 14 has already been [drafted](https://github. (com / cplusplus / draft), but considering 2014, at least C ++ 11 or later compliant code Would be good to write.

The following is an example of using C ++ 11 std :: array. The default array has less functionality than std :: vector. On the other hand, std :: array wraps the raw array so that it can be used like std :: vector. Since it is a wrapper, it has the feature that the internal cost and speed are almost the same as the original array.

#include <algorithm>
#include <functional>
#include <array> // std::array
#include <iostream>

int main()
{
    std::array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; // std::Array by array

    std::sort(s.begin(), s.end(), std::greater<int>()); //Std for array sorting::Use sort
    for (int a : s) {
        std::cout << a << " "; //Sort and export to standard output
    }
    std::cout << '\n';
}

Detailed techniques for using C ++

zsh's alias -s allows you to assign a function to the extension, which allows you to use C ++ as if it were a scripting language.

function runcpp () { g++ -std=c++11 $1 && shift && ./a.out $@ } # -std=c++C at 11++11 Compliant
alias -s {c,cpp}=runcpp #Assign a function for the extension

After that, if you start the source code as if the execution attribute is given like ./my.cpp, the processing result will be returned.

Please note that C ++ 11 is compliant with g ++ 4.7 and later. It may not work as-is with older distributions of packages such as CentOS 6. (For CentOS 6, it will be Available by adding repository)

Summary

Python is famous as a glue language, but if you write the essential part with an extension library using C ++, you can also speed up the bottleneck. For example, when implementing a new statistics / machine learning library, it may be more suitable to use an extension library than to write it natively, considering the realistic speed. The range of applications will expand.

Recommended Posts

Implement part of the process in C ++
Implement the solution of Riccati algebraic equations in Python
Implement Gaussian process in Pyro
Measure the execution result of the program in C ++, Java, Python.
About the garbled Japanese part of pandas-profiling in Jupyter notebook
The story of participating in AtCoder
Implement the Singleton pattern in Python
Process the result of% time,% timeit
The story of the "hole" in the file
The meaning of ".object" in Django
How to implement Java code in the background of RedHat (LinuxONE)
[Understanding in 3 minutes] The beginning of Linux
Check the behavior of destructor in Python
Implement FIR filters in Python and C
The result of installing python in Anaconda
Let's claim the possibility of pyenv-virtualenv in 2021
I wrote the selection sort in C
The basics of running NoxPlayer in Python
Understand the "temporary" part of UNIX / Linux
In search of the fastest FizzBuzz in Python
Set the process name of the Python program
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
A story about trying to improve the testing process of a system written in C language for 20 years
Try installing only the core part of Ubuntu
How to use the C library in Python
Output the number of CPU cores in Python
The meaning of {version-number} in the mysql rpm package
[Python] Sort the list of pathlib.Path in natural sort
Change the font size of the legend in df.plot
Match the distribution of each group in Python
View the result of geometry processing in Python
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Make a copy of the list in Python
Find the number of days in a month
Read the output of subprocess.Popen in real time
Find the divisor of the value entered in python
Send Gmail at the end of the process [Python]
[Python] Read the source code of Bottle Part 1
Implement the mathematical model "SIR model" of infectious diseases in OpenModelica (see the effect of the vaccine)
The story of finding the optimal n in N fist
Getting rid of DICOM images in Python Part 2
Fix the argument of the function used in map
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
Tucker decomposition of the hay process with HOOI
Put the second axis in 2dhistgram of matplotlib
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Visualized the usage status of the sink in the company
Output in the form of a python array
Get only the address part of NIC (eth0)
The story of viewing media files in Django
Search by the value of the instance in the list
Implement the REPL
Make progress of dd visible in the progress bar
Factfulness of the new coronavirus seen in Splunk
I implemented Human In The Loop ― Part ① Dashboard ―
[Python + OpenCV] Whiten the transparent part of the image
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
How to display the modification date of a file in C language up to nanoseconds