|
|
|
|
|
by srean
4320 days ago
|
|
The 'cheering' part was not directed at you, I should have made that clearer and indeed my comments are specific to numeric code. You mention Java, it so happened that I commented why Java is not a good solution for numerics just yesterday https://news.ycombinator.com/item?id=8214922 However, if you follow that thread you would see that it is not clear yet whether writing C is what would give you the most performant and correct code. > The argument of preferring writing C code to Cython code is moot because you have the choice of writing code in Cython or using it to wrap existing C code. I am not convinced about this and I have mentioned why I am not so enamored by this approach. Although quite a feat in itself Cython is not a very sophisticated compiler, so if you are writing C code you are better of taking charge and writing the C yourself. You get to enjoy the tooling that have accumulated over the years around the C syntax language. Otherwise you often get yourself in a solution that you have to debug the autogenerated C, not very pleasant. The second reason is that the bridge between Cython and C is by no means cheap. Note Cython's objective is to produce a Python C modules, not native C binaries, so it will talk to see with all the disadvantages of talking to C from a Python C module. Cython is indeed great if you have legacy code in Python that you would want to marry to C, or to get some speedup with minimal intervention, but if you are not so constrained and want more speedup than this affords perhaps better approaches are needed. I would also point out that Julia does not always yield faster solutions than Cython yet. My main motivation was to point out some design issues that you are saddled with when you are a denizen in the Python world. |
|
As for debugging, I admit, it can get ugly with Cython, but in my experience this only happens when you decide to do low level manual memory management. This ability to shoot yourself in the foot is part of the trade-off of close-to-the-metal performance. It's not pretty but then again descending to that level is entirely optional.
You say that the bridge between C and Cython is not cheap, but this characterization is mistaken. It is easy to write Cython code that maps 1-to-1 to C code without using any part of Python whatsoever. What is expensive is bridging back to Python; e.g., calling a Python function requires constructing a tuple and all Python objects are heap allocated. However, you can choose to use this bridge as little as you want (the extreme case is only using Python to call a main function defined in Cython).
You argue that Cython only produces modules but not native binaries. In fact this is not true, it can produce such binaries but that implies including a Python interpreter as part of the binary (--embed option).
> and want more speedup than this affords perhaps better approaches are needed.
What are you alluding to here? Cython offers the speed of pure C/Fortran. I can think of 2 limitations: calling back and forth from C code to Python code is expensive (but then don't do that frequently, if it's a tight loop it's worth optimizing), and JIT optimizations.