[C++-sig] numpy integration with boost.python

heroxbd at gmail.com heroxbd at gmail.com
Wed Dec 4 16:05:00 CET 2013


Dear all,

This is a cross post of stackoverflow[0].

I am interfacing a C++ data intense library with Python by
py++[1]/boost.python[2]. After profiling my program, I find 70% of run
time is spent on code like this:

    ni = range(v2o.getHits())
    tau = np.array([v2o.TofCorrectedTime[i] for i in ni])
    q = np.array[v2o.getCharge()[i] for i in ni]

v2o.TofCorrectedTime is typed __array_1_float_2368[3] from
py++. v2o.getCharge() is typed _impl_details_range_iterator_[4] from
py++, too. Size being about 2000, the convertion from these py++ array
wrappers to numpy is slow:

    In [42]: timeit np.array(v2o.TofCorrectedTime)
    100 loops, best of 3: 2.52 ms per loop

    In [43]: timeit np.array(v2o.getCharge())
    100 loops, best of 3: 4.94 ms per loop

    In [44]: timeit np.array([0]*2368)
    1000 loops, best of 3: 310 µs per loop

    In [45]: timeit np.array(np.zeros(2368))
    100000 loops, best of 3: 4.41 µs per loop

I searched the web for a solution. The candidates are:

 1. Cython[5] with memoryview[6]
 2. pyublas[7]
 3. Boost.NumPy[8]

Questions:
 
 - Is cython/memoryview easy to be integrated with boost.python and
   py++? I want to keep the rest of the library wrapper.

 - Which one best suites my problem in terms of convertion overhead?

Thanks,
Benda

  [0]: http://stackoverflow.com/questions/20376021/exchange-numpy-array-with-boost-python-pyublas-or-boost-numpy
  [1]: http://svn.code.sf.net/p/pygccxml/svn/pyplusplus_dev
  [2]: http://www.boost.org/libs/python
  [3]: http://svn.code.sf.net/p/pygccxml/svn/pyplusplus_dev/pyplusplus/code_repository/array_1.py
  [4]: http://svn.code.sf.net/p/pygccxml/svn/pyplusplus_dev/pyplusplus/code_repository/return_range.py
  [5]: http://cython.org
  [6]: http://docs.cython.org/src/userguide/memoryviews.html
  [7]: http://mathema.tician.de/software/pyublas/
  [8]: https://github.com/ndarray/Boost.NumPy


More information about the Cplusplus-sig mailing list