12 ms·
How Numba and Cython speed up Python code
- metalliqaz 8y agoI 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.
- abakus 8y agoThey usually cannot improve carefully optimized numpy operations, nor GPU operations. The use cases are thus limited.
- nerdponx 8y agoYou can use Numpy in Cython.
- geoalchimista 8y agoSecond this. Cython's memory view for numpy array is great.
- Paul-ish 8y agoIn particular, if you find you cannot use vectorized functions in numpy or scipy and absolutely MUST index, then typing the array in Cython is a life saver. Indexed operations on numpy arrays without Cython is very slow. (eg https://stackoverflow.com/q/22239199/300539 https://stackoverflow.com/q/22239199/300539)
- geoalchimista 8y agoAgree. It's a bit surprising to see that numpy indexed operations are even slower than the built-in list in this example. It seems the idiomatic numpy way to perform the iterations is through vectorization, but that often leads to code that is not straightforward to reason about. For this example, I'd prefer the simplicity of the Cython for-loop when it comes to optimization.
- infinite8s 8y agoThat's because with generic python code the values in a numpy array need to be boxed into python objects leading to extra memory allocations (whereas they would already be boxed in the built-in list case).
- 8y ago
- mlevental 8y agois cython compatible with all python code or is it only a subset of libraries that you can use?
- f311a 8y agoYou can import whatever you want, but it will work in "Python" mode.
- deathanatos 8y agoIt depends. This is the only deviation I've ever found: https://github.com/cython/cython/issues/1936 https://github.com/cython/cython/issues/1936 ; that is, Cython will execute __prepare__ in Python 2; but __prepare__ doesn't exist in Python 2, so the normal interpreter (CPython) won't execute it. This can lead to deviations; in my case, the code crashes if run under Cython, and executes fine under CPython. The Cython maintainers disagree with me that this is a bug, so, if you're under Python 2, I would say it is "very nearly" compatible. If you're in a recent version 3, AFAICT, it just makes Python code faster.
- jebej 8y agoJust use julia instead!
- goatlover 8y agoThis. If you need to drop out of regular Python for performance reasons, then Julia offers the same high level flexibility, but with types and performance.
- auntienomen 8y agoCython basically lets you sprinkle types and performance into your Python code, as needed. It's not as drastic a departure as switching languages.
- ssivark 8y agoThere's a reason most high-performance Python libraries are not written that way, and core routines are just written in C instead. Proof of the pudding is in the eating! See this talk by Armin Ronacher (creator of Flask) on why the design of python makes it fundamentally unfriendly to performance optimizations: https://youtu.be/qCGofLIzX6g?t=171 https://youtu.be/qCGofLIzX6g?t=171 Julia has be designed ground up to avoid several such problems. See this discussion: https://discourse.julialang.org/t/julia-motivation-why-werent-numpy-scipy-numba-good-enough/2236 https://discourse.julialang.org/t/julia-motivation-why-weren... If your domain falls under the umbrella of numerical and scientific computing, writing Julia is as painless as writing python, with code that automatically runs roughly as fast as C. If you're used to writing numpy, you can hit the ground running in Julia, with maybe a few hours to become comfortable with the slightly different syntax and the names of useful libraries.
- jzwinck 8y ago> There's a reason most high-performance Python libraries are not written [with Cython], and core routines are just written in C instead. Pandas, Scipy and lxml are large, very popular Python libraries that use Cython. The article even mentions them at the end.
- peheje 8y agoNim is a language that mimicks python syntax but compiles down to c. Really cool and easily gives you 'fast code'. If you are into this kind of stuff I would recommend you take a look. https://nim-lang.org/ https://nim-lang.org/
- coleifer 8y agoHow's the standard library in nim? What's the 3rd party package ecosystem like? If I have questions will I be able to find a good answer on Google? How about jobs? There's a handful of languages out there you could consider "faster python". I just use cython though.
- peheje 8y agoPython is awesome don't get me wrong. I have limited knowledge about it, but mainly it isn't version 1.0 yet so the std lib stills changes and has some quirks/missing pieces or pieces that are not quite aligned yet. The package system is Nimble, seems easy enough, fetches packages from github given a version, though I'm not sure how secure that is. Not sure about Google as its relatively new, but in general the Nim forum is pretty active. I have asked around there a few times and got answers even from the language creators themselves.
- tasty_freeze 8y agoI haven't used it, but nuitka (http://nuitka.net/ http://nuitka.net/) translates python (2.6, 2.7, 3.3 to 3.7) to C and compiles that. It claims to be highly compliant and performant without any extra pragmas. How does is compare to the two technologies in the article?