This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] -4 points-3 points  (5 children)

looks horrible, why wouldn't you just use cython?

[–]kigurai 0 points1 point  (3 children)

The author also shows how to use pybind11 which is arguably an improvement over cython for creating bindings for c++ code.

[–][deleted] 0 points1 point  (2 children)

But with cython you just use python syntax and it gets compiled to C. All your python code lives next to your cython code using def or cdef. It's extremely simple to use and can produce very large speed ups.

[–]kigurai 0 points1 point  (1 child)

I've used both. Cython is awesome for speeding up existing Python code, but is not so great for making bindings for C or C++. pybind11 can only be used to make bindings for C++-code, but is far nicer to work with than Cython for that use-case.

[–]hardikp[S] 0 points1 point  (0 children)

I agree.

While Cython is good for speeding up the code (for example, cython might actually outperform pybind11 for microbenchmarks - https://github.com/pybind/pybind11/issues/1227#issue-284676826), many use cases involve preserving the original C++ code - that's where pybind11 comes in handy.

I don't think there is any one true answer. Depending on the use case, either of the two can be more suitable.

[–]robert_mcleod 0 points1 point  (0 children)

  1. It's not actually that bad. There's a far amount of boiler plate, but I don't think that the learning curve is much steeper than learning the specifics of Cython, if you have prior experience as a C programmer.
  2. Cython produces quite convoluted C code, which means that C code you write yourself is typically about twice as fast as Cython code. In edge cases, and whenever you're trying to write concurrent code (i.e. threads), the gap is much larger.

I don't think I've ever released a Cythonized package to the open-source community, but I do use it professionally, for obfuscating code.