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 ++.
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.
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.
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 */
};
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 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')
For the official C ++ standard, Read the standard is recommended. Alternatively, you can refer to C ++ reference book is available.
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;
}
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';
}
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)
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