4 ms·
I like Cython, but I'm always surprised at how willing programmers seem to be to use the cython syntax. In so many cases I look at the optimized program and th
by metalliqaz 8y ago
I like Cython, but I'm always surprised at how willing programmers seem to be to use the cython syntax. In so many cases I look at the optimized program and think to myself, "I would understand that better if was just written in C."
- pebers 8y agoI agree, my vastly preferred option now is Python code and C code with cffi to glue them together. I find that much preferable to Cython which is basically a third language altogether (and IMO not an especially nice one, although that is a bit of a value judgement).
- dagw 8y agoThe huge advantage with cython compared to C is that it is trivial to call python libraries and get the same behaviour as you would in python.
- nerdponx 8y agoCython caters to Python programmers who don't know C well enough to be comfortable writing C (like me).
- pard68 8y agoAgreed. I tried Cython but decided against using it. I now proof test in Python and rewrite in C.
- mlthoughts2018 8y agoI couldn’t disagree more. I find Cython syntax allows me to express the thing I want at the C-level with significantly less work than the equivalent syntax directly in C. For example consider using cdef to define a simple class (which compiles directly to a struct + helper functions). Organizing it with class-like syntax is so much easier and better mapped to the concept model, and (as it should be) the compiler worries about how to map that concept down to a thin struct with functions (something the programmer should not have to consciously think about but should still benefit from). You can go really far with this with Cython. For example this is a side project to write a “typeclass”-like pattern for polymorphism in Cython. https://github.com/spearsem/buffersort https://github.com/spearsem/buffersort The function definitions hardly look different from plain Python, yet you’re getting auto-generated specialized functions for each resolvable child type of the type class (called “Ord” here), plus a dispatcher to invoke them from Python. So you sort of get just as much type dynamism as plain Python, except each typed instance is much more performant with no PyObject or PyFunction overhead in the call sequence.
- kevin_thibedeau 8y agoEven if you write the Cython naively (PyObjs everywhere), you get a modest speed boost simply by cutting out the Python bytecode interpreter. This is a very low friction process as opposed to carving out chunks of code that can be isolated from the Python runtime.
- jhayward 8y agoThis is probably because you don't use much of the Python ecosystem in your C module. If you're trying to speed up something that both calls and is called from Python then Cython saves a lot of verbiage and some fiddly bits.