Class 9 - extension modules
Python code at the end of class: class9_inclass.zip.
- We wrote in C a simple extension module that calculates n-th Fibonacci number and compiled it with setuptools.
- We covered ways to speed up Python code by using Cython, nanobind, nanobind with Eigen3 library, as compared to Python with NumPy and pure C code. Other C++ libraries providing multidimensional array object could also be made to work with nanobind relatively easily, since it provides a data pointer and shape, stride information necessary to wrap it in an object.
To compile nanobind modules we used cmake -S . -B build
and cmake --build build
commands and then copied the resulting dynamic library from build
directory to src/c_for_speed
directory. To compile cython modules we used python setup.py build_ext --inplace
command and then copied the resulting library to src/c_for_speed
directory. To make use of these modules, we needed to install the package once in editable mode with pip install -e .
.