Example project that shows hybrid programming with C++ and Python. It also shows how to wrap C++ datatypes as NumPy arrays, so as to enable use of SciPy routines.
- Cmake version 3.2 or newer
- C++11 compatible compiler
- Boost.Python version 1.63
- Python 3
- NumPy and SciPy
- Tested on an Ubuntu 16.04 LTS.
See towards the end of this file for instructions how to install these.
git clone git@github.com:dzhoshkun/cpp-py-example.git
cd cpp-py-example/ex && export PYTHONPATH="$(pwd):$PYTHONPATH" && cd ../..
mkdir cpp-py-example-build && cd cpp-py-example-build
cmake ../cpp-py-example/src
make -j
export PYTHONPATH="$(pwd):$PYTHONPATH"
Most of the *.py
files in the ex
sub-folder contain main functions.
Some of these use the image file located in the res
sub-folder.
So please change to this folder before running any example.
You can then run any example by calling the file directly, e.g. ../ex/gil.py
.
The mgrad.py
file in the ex
folder is intended for benchmarking.
You can benchmark the pure Python function in this file using e.g.:
python3 -m timeit "from mgrad import grad; import numpy as np; arr = np.zeros((128, 128, 3), dtype='uint8'); grad(arr)"
To benchmark its C++ implementation, replace mgrad
with pymycpp
, i.e:
python3 -m timeit "from pymycpp import grad; import numpy as np; arr = np.zeros((128, 128, 3), dtype='uint8'); grad(arr)"
- Ubuntu:
apt install cmake
- Mac OS X:
brew install cmake
(i.e. using Homebrew)
- Ubuntu
- Install Python 3 development libs (see below).
wget https://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.bz2
tar xvfj boost_1_63_0.tar.bz2
cd boost_1_63_0
./bootstrap.sh --with-python=$(which python3)
./b2 -j 7
./b2 -j 7 install
- Mac OS X
- Install Python 3 and SciPy (see below)
brew install boost-python --with-python3
- You might need to set your
BOOST_ROOT
, e.g.export BOOST_ROOT=/usr/local/Cellar/boost/1.63.0/include
- Ubuntu:
apt install python3-dev
- Mac OS X:
brew install python3
- Ubuntu
apt install python3-numpy
apt install python3-scipy
- Mac OS X:
brew install scipy --with-python3