Comparison to boost.python

781 views
Skip to first unread message

Francisco Paisana

unread,
Dec 7, 2014, 7:52:07 AM12/7/14
to cython...@googlegroups.com
What are in your opinion the main strengths of cython C++ wrapping compared to the boost.python lib. I have only encountered the views of boost.python users on other forums so I would like to hear the opinion of some programmers of this group.

In my opinion, cython has a way better syntax than the ugly C++ boost lib, and it is way easier to understand the tutorials. The main thing I would change is describe more clearly how the conversion of stl containers is handled. I have also heard that boost.python is slow at compiling. Is it true?

Any way, I would like to hear your views.

Thanks.

Regards,
Francisco

Robert Bradshaw

unread,
Dec 9, 2014, 12:27:27 PM12/9/14
to cython...@googlegroups.com
I think the biggest difference is that boost.python is a way to write
wrappers using C++ syntax, Cython is a way to write wrappers using
Python syntax, which influences both syntax and the perspective taken
and volume of extra details to handle when doing the wrapping. Part of
this is a matter of personal preference, but there are a couple of
other objective differences.

* Cython generates faster code than boost.python, at least in all the
latest benchmarks I've seen.

* Cython is generally faster to compile, especially if you're just
wrapping a couple of plain C functions, probably in large part to
boost's heavy use of templates.

* Cython allow you to improve your code incrementally, if something
isn't fast enough just move it to a .pyx file and measure again.
Profile to see where your bottleneck is and add a type here or there.
The boundary between low and high level code is much more flexible
(both in mechanics and in style).

* Cython is designed for writing more than just wrappers. This IMHO
makes it easy to write Pythonic bindings that are more than simply 1:1
exposures of the underlying C++ API. When doing so you can use
arbitrary Python language features.

Though the first two could be addressed by improving boost.python,
eventually, but we're a moving target too. It'd be harder to do
anything about the latter two which I think is the bigger win.

On the other hand

* Boost.python probably has better support for stl containers. For
Cython, there is a fixed number of automatic conversions that we
support (e.g. integer types, correctly handing overflow and
signedness) and handle stl containers that are (recursively) composed
of these.

* Boost.python seems to have better support for exposing C++ classes
as Python classes. With Cython you actually create a Python class that
explicitly wraps a C++ instance. This is both a pro and con (see the
last two advantages points above).

- Robert

Francisco Paisana

unread,
Dec 9, 2014, 8:26:46 PM12/9/14
to cython...@googlegroups.com
Thanks Robert for the answer. Maybe another point in favour of boost.python is these side projects that wrap C++ classes automatically. To the best of my knowledge, there is very limited support for this feature in cython. To be fair, even without this feature, I still prefer the cython way because cython/python syntax is way more concise and easier to understand. Seriously, I have been using C++ for quite a while now and C++ code can sometimes look horrible, especially when using external libraries, like boost.

Ian Bell

unread,
Dec 10, 2014, 1:52:46 AM12/10/14
to cython...@googlegroups.com
On Tue, Dec 9, 2014 at 8:26 PM, Francisco Paisana <pais...@tcd.ie> wrote:
Thanks Robert for the answer. Maybe another point in favour of boost.python is these side projects that wrap C++ classes automatically. To the best of my knowledge, there is very limited support for this feature in cython. To be fair, even without this feature, I still prefer the cython way because cython/python syntax is way more concise and easier to understand. Seriously, I have been using C++ for quite a while now and C++ code can sometimes look horrible, especially when using external libraries, like boost.

There is also xdress for auto-wrapping of C++ (https://github.com/xdress/xdress/releases), but I have found it difficult to use and not stable.  In the end, for the python wrapper of my library, I have hand-build the wrapper.  Its a PITA to maintain, but it works.

Ian 

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Bradshaw

unread,
Dec 10, 2014, 1:53:07 AM12/10/14
to cython...@googlegroups.com
On Tue, Dec 9, 2014 at 5:26 PM, Francisco Paisana <pais...@tcd.ie> wrote:
> Thanks Robert for the answer. Maybe another point in favour of boost.python
> is these side projects that wrap C++ classes automatically. To the best of
> my knowledge, there is very limited support for this feature in cython.

True. There's a couple of options, most notably xdress, but none are
fully complete/mature: https://github.com/cython/cython/wiki/AutoPxd

Neal Becker

unread,
Dec 10, 2014, 8:23:52 AM12/10/14
to cython...@googlegroups.com
Robert Bradshaw wrote:

> On Sun, Dec 7, 2014 at 4:52 AM, Francisco Paisana <pais...@tcd.ie> wrote:
>> What are in your opinion the main strengths of cython C++ wrapping compared
>> to the boost.python lib. I have only encountered the views of boost.python
>> users on other forums so I would like to hear the opinion of some
>> programmers of this group.
>>
>> In my opinion, cython has a way better syntax than the ugly C++ boost lib,
>> and it is way easier to understand the tutorials. The main thing I would
>> change is describe more clearly how the conversion of stl containers is
>> handled. I have also heard that boost.python is slow at compiling. Is it
>> true?
>>
>> Any way, I would like to hear your views.
>
> I think the biggest difference is that boost.python is a way to write
> wrappers using C++ syntax, Cython is a way to write wrappers using
> Python syntax, which influences both syntax and the perspective taken
> and volume of extra details to handle when doing the wrapping. Part of
> this is a matter of personal preference, but there are a couple of
> other objective differences.
>
> * Cython generates faster code than boost.python, at least in all the
> latest benchmarks I've seen.

I don't have any benchmarks to compare, but I'm very surprised to hear this. I
don't see any fundamental reason why this should be true.


I'd like to add my own opinion:

* It is more clear what exactly is happening in boost::python than in cython.
In cython (and a lot of other projects such as numba and julia), the compiler
might or might not generate fast code. When it is not fast, it can be difficult
to guess why and fix it. I believe for all it's faults, boost::python is
actually more transparant in this respect. Or, maybe I'm just more familiar
with it :)


-- Those who don't understand recursion are doomed to repeat it

Robert Bradshaw

unread,
Dec 10, 2014, 12:14:56 PM12/10/14
to cython...@googlegroups.com
On Wed, Dec 10, 2014 at 5:23 AM, Neal Becker <ndbe...@gmail.com> wrote:
> Robert Bradshaw wrote:
>
>> On Sun, Dec 7, 2014 at 4:52 AM, Francisco Paisana <pais...@tcd.ie> wrote:
>>> What are in your opinion the main strengths of cython C++ wrapping compared
>>> to the boost.python lib. I have only encountered the views of boost.python
>>> users on other forums so I would like to hear the opinion of some
>>> programmers of this group.
>>>
>>> In my opinion, cython has a way better syntax than the ugly C++ boost lib,
>>> and it is way easier to understand the tutorials. The main thing I would
>>> change is describe more clearly how the conversion of stl containers is
>>> handled. I have also heard that boost.python is slow at compiling. Is it
>>> true?
>>>
>>> Any way, I would like to hear your views.
>>
>> I think the biggest difference is that boost.python is a way to write
>> wrappers using C++ syntax, Cython is a way to write wrappers using
>> Python syntax, which influences both syntax and the perspective taken
>> and volume of extra details to handle when doing the wrapping. Part of
>> this is a matter of personal preference, but there are a couple of
>> other objective differences.
>>
>> * Cython generates faster code than boost.python, at least in all the
>> latest benchmarks I've seen.
>
> I don't have any benchmarks to compare, but I'm very surprised to hear this. I
> don't see any fundamental reason why this should be true.

Fundamentally, they all share a common upper bound in speed. Doesn't
mean they all (even close to) achieve it.

http://www.behnel.de/cycppbench/ is several years old. I wasn't able
to get http://bazaar.launchpad.net/~gjc/pybindgen/trunk/files/735/benchmarks/
to build, maybe someone else will have better luck?

> I'd like to add my own opinion:
>
> * It is more clear what exactly is happening in boost::python than in cython.
> In cython (and a lot of other projects such as numba and julia), the compiler
> might or might not generate fast code. When it is not fast, it can be difficult
> to guess why and fix it. I believe for all it's faults, boost::python is
> actually more transparant in this respect. Or, maybe I'm just more familiar
> with it :)

A similar argument could be made for assembly :).

- Robert

Daπid

unread,
Dec 10, 2014, 1:05:50 PM12/10/14
to cython...@googlegroups.com
On 10 December 2014 at 14:23, Neal Becker <ndbe...@gmail.com> wrote:
>> * Cython generates faster code than boost.python, at least in all the
>> latest benchmarks I've seen.
>
> I don't have any benchmarks to compare, but I'm very surprised to hear this. I
> don't see any fundamental reason why this should be true.

I have no experience with boost, but I couldn't measure differences in
runtime between a Cython without any Python calls and the GIL
released, and the same code implemented in C++. The function in
question was around 70 lines of code, including reading and writing
data from a memoryview/std::vector and a lot of arithmetic; and with a
runtime of the order of miliseconds. The same code with GIL was
slightly slower, but I don't remember how much. So, I think that for
at least the most basic cases (straightforward, procedural, static,
and with all arguments explicit), Cython is as fast as it can get.


/David.

Stefan Behnel

unread,
Dec 11, 2014, 1:42:42 AM12/11/14
to cython...@googlegroups.com
Neal Becker schrieb am 10.12.2014 um 14:23:
> Robert Bradshaw wrote:
>> * Cython generates faster code than boost.python, at least in all the
>> latest benchmarks I've seen.
>
> I don't have any benchmarks to compare, but I'm very surprised to hear this. I
> don't see any fundamental reason why this should be true.

I think Robert was referring to the wrapper code, i.e. code that links
between Python and C/C++ by mapping calling conventions and converting
data. Here, Cython has been heavily tuned over the years and I haven't seen
any other (static) wrapping tool that shows similar performance. It should
even beat hand-written code in some cases, especially when it comes to
calling between Python and Cython.

It's obvious that for pure C or C++ code, both Cython and Boost should
perform pretty much the same as the speed depends more on the C/C++
compiler here than anything else..

Stefan

Reply all
Reply to author
Forward
0 new messages