9 ms·
Cython is 20
- kubb 4y agoand i still have no idea what i could use it for...
- ergo14 4y agoWe speed up our ML code 40 times using it.
- DaedPsyker 4y agoWould that be in the data loading that you are getting the most benefit? I'm curious, since most of the big libraries are already just cuda calls anyway but I'm always interested in anything to speed up the full process.
- ergo14 4y agoIn this case was multicore computation without GIL if i remember correctly.
- microtonal 4y agoI can't speak for the parent commenter, but there is often code processing the input/output of machine learning models that benefits from high-performance implementations. To give two examples: 1. We recently implemented an edit tree lemmatizer for spaCy. The machine learning model predicts labels that map to edit trees. However, in order to lemmatize tokens, the trees need to be applied. I implemented all the tree wrangling in Cython to speed up processing and save memory (trees are encoded as compact C unions): https://github.com/explosion/spaCy/blob/master/spacy/pipeline/_edit_tree_internals/edit_trees.pyx https://github.com/explosion/spaCy/blob/master/spacy/pipelin... 2. I am working on a biaffine parser for spaCy. Most implementations of biaffine parsing use a Python implementation of MST decoding, which is unfortunately quite slow. Some people have reported that decoding dominates parsing time (rather than applying an expensive transformer + biaffine layer). I have implemented MST decoding in Cython and it barely shows up in profiles: https://github.com/explosion/spacy-experimental/blob/master/spacy_experimental/biaffine_parser/mst.pyx https://github.com/explosion/spacy-experimental/blob/master/...
- dagw 4y agoIt's great for speeding up 'hot' functions in your python code and makes it easy to call C libraries from python.
- baq 4y agoit's C with Python syntax and syntactic sugar for Python objects on C level, including refcounting, which is the hard part. if you successfully use numba, probably nothing that you couldn't already do. if you want something that lives much closer to C, it's perfect.
- hansor 4y agoWe had to parse dozeon of 20GB files daily with super complex structure and not in linear structure. With Cython (finally we migrated to Pypy) we gained around 20-60x speedup.
- nurbl 4y agoIt's easy to drop in Cython in an existing project where you need some performance, and start gradually "cythonizing" modules from the inside out. The rest of the code does not need to care. With a bit of care (and benchmarking) you can get very respectable speed. The main drawback is that the further you go, the more C knowledge you need in order to not blast your own feet off. If you're just after a bit more performance in general, a drop in solution like pypy might be enough.
- physicsguy 4y agoI love Cython. I really feel like it's the right balance of usability and allowing you to do what you want/need. Want to make your code a bit faster? Write Python with type annotations. Want to call a C library? Just import the header, and then use it from a function. Pybind11 is also great, but quite different in aims - I feel like it's more like a project for C++ programmers wanting to expose functionality to Python.
- jokoon 4y agoAny benchmarks for type annotations? I already wrote a few patch for pysfml, which is written in cython, it was a bit awkward, and now I'm asking myself if cython is really the right tool to write bindings, compared to cpython, for example.
- physicsguy 4y agoGonna depend a huge amount on what you're doing to be honest. I used it for physics modelling codes and it made a bit of a difference (comparable to Numba) but dropping to C for the main computation routines was what we ended up doing, and that worked very well for us. It's very fast to write for, that's the main benefit. Use it together with profiling and just pick off the slowest part first.
- tristan957 4y agoFor what it's worth I wrote Python bindings using Cython for our open source C-API storage engine and the performance was fairly close to on par with C.
- machinekob 4y agoPython with type annotations isnt faster using python runtime. But some packages can utilize it for higher performance but most of the time it'll be slower cause you need to parse extra information if you want to reuse it in pure python.
- quietbritishjim 4y ago
- c-fe 4y agoI have heard about Cython before but I have never actually used it. I have however used Numpy, Scipy and Numba. Are there any reasons to also consider Cython in combination with those other libraries? E.g. in which cases would Cython be considerably better than Numpy or Numba? My workload consists mostly of data science and statistics, running models and simulations.
- dagw 4y agoCython works great in conjunction with Numpy arrays and you can easily call numpy and scipy methods from within Cython. The big win comes when you have to do some operation to a numpy array that doesn't have a 'fast' path within numpy. If you ever find yourself in a situation where you have to loop over or apply any sort of custom operation to every element in a numpy array then Cython can be a huge win, especially since Cython also makes it possible to parallelise those loops. The other place it shines is if you ever need to loop over an array of data that cannot easily be represented as numpy arrays, like strings or more complex structs. Here you can get significant speedups compared to python. The third use of Cython I really like is with C and C++ interop. Sure there are lots of ways of calling C code from Python, but to me Cython is probably the quickest and cleanest. Compared to Numba, it's harder to say. Numba, when it works, is easily as fast as Cython. However I find Numba hard to reason about and it's still a bit of a black box as to when and why it does and doesn't work. The nice thing about Cython is that it is pretty simple so you can easily reason about what it will do your code and how it will perform. It's been a long time since Cython 'surprised' me by performing much better or worse than I expected. If you want to see Cython in action, take a look at the source code of scikit-image or scikit-learn. They implement many of their core algorithms in Cython
- nicoco 4y agoNumba is better in my opinion for the use case you describe, less hassle. However, (I think) cython is superior when: - you want to distribute (eg as a pypi package) your code - you want to interface with C/C++ code libs I found out I almost never have to do this and did not touch cython since I started using numba.
- rich_sasha 4y ago
- pjmlp 4y agoWhile it is nice that this option is available, it would be much better if Python itself would embrance the necessary runtime capabilties to not have to rely on it.
- fname11 4y agoThis is not going to happen. GvR has successfully ignored Cython and PyPy for decades and has attached himself to a JIT project at Microsoft (has anything emerged?). CPython is in the hands of not really productive bigcorp representatives who care about large legacy code bases. My guess is that CPython will be largely the same in 10 years, with the usual widely hyped initiatives that go nowhere ("need for speed etc.").
- linspace 4y ago> who care about large legacy code bases It's clear that Python's main strength is its vast libraries, priority number one is not breaking them. If it could be possible to speed up Python without breaking changes I would be surprised precisely because with so much large codebases speed and efficiency would translate directly to money.
- dikei 4y agoYeah, it took over a decade to switch from Py2 to Py3 due to the breaking changes it brought. I'd rather not to have such a large change again, ever.
- poulpy123 4y agoThey really missed an opportunity when they made the switch from py2 to 3 to break things a bit more but give more improvement in exchange
- linspace 4y agoCompletely agree. I think GvR was too conservative. And yet he had lot of backslash, it's easy for me now to criticize several years later after the fact, I only have respect for the work done. I think it was in the mind of everyone the Perl 5/Perl 6 transition.
- jokoon 4y agoQuestion: I found python "bindings" for SFML, written in cython, and patched them a bit. I guess Cython is not really made to write bindings, but is it easier to write bindings with cython or cpython?
- Galanwe 4y agoAs someone who has been writing python bindings regularly for 10 years: Writing bindings in Cython is much, much faster in terms of development time. It fits nicely and unintrusively in an already python packaged library. You can gently add some C functions or call C libraries in minutes. You won't have a full control of what's happening though. Just have a look at the generated code and you'll see the mess of indirections that are generated. Cython bindings become limited when you have to build more complex stuff though, going deeper than just calling some C functions. The typical case is when you have to actually handle the lifetime and borrowing of C native objects. At that point, CPython will be the way to go, but it's much more code, and very error prone: you have to manually keep track of reference counting.
- fermigier 4y agoI made an "Awesome Cython" page last year. I welcome pull requests (or you can fork it as you want): https://github.com/sfermigier/awesome-cython https://github.com/sfermigier/awesome-cython
- erwincoumans 4y agoI would recommend considering using NanoBind, the follow up of PyBind11 by the same author (Wensel Jakob), and move as much performance critical code to C or C++. https://github.com/wjakob/nanobind https://github.com/wjakob/nanobind If you really care about performance called from Python, consider something like NVIDIA Warp (Preview). Warp jits and runs your code on CUDA or CPU. Although Warp targets physics simulation, geometry processing, and procedural animation, it can be used for other tasks as well. https://github.com/NVIDIA/warp https://github.com/NVIDIA/warp Google Jax is another option, jitting and vectorizing code for TPU, GPU or CPU. https://github.com/google/jax https://github.com/google/jax
- beltsazar 4y agoOr alternatively, PyO3 if you use Rust instead of C++: https://github.com/PyO3/pyo3 https://github.com/PyO3/pyo3
- logicchains 4y ago>I would recommend considering using NanoBind, the follow up of PyBind11 by the same author (Wensel Jakob), and move as much performance critical code to C or C++ Why would you recommend that? It's all way more effort than just writing Cython, especially in a Jupyter Notebook. And Cython code can be just as fast as C/C++ code unless you're doing something really fancy. It's a bunch of work for no benefit. >Warp jits and runs your code on CUDA or CPU If someone's writing Cython it's probably because they found something that couldn't be done efficiently in Numpy because it was sequential, not easily vectorisable. Such code is going to get zero benefit from Cuda or running on the GPU. In general, all your jitted code is not going to be as fast as code compiled with an ahead-of-time compiler like the C compiler that Cython uses. Moreover if you use a JIT then it makes your code a pain in the ass to embed in a C/C++ application, unlike Cython code.
- erwincoumans 4y agoWarp generates C/C++ code, that can be trivially used in a pure C++ or CUDA project without issues. So it is not strictly jit, since it calls the regular ahead-of-time compiler (gcc, llvm or nvcc) only when de code changes (using hashes to check for changes), so performance is good. Also, random non-vectorizable branchy code will run fine on cpu with Warp, but you loose many benefits indeed. Agreed, if you have bad performing spaghetti Python code, none of those tools are going to help indeed. Then I would rather rewrite it all in C/C++ instead of fiddling with Cython.
- Bostonian 4y agoIs the 2015 O'Reilly book on Cython by Kurt Smith still a good starting point to learn about it, or is it outdated?
- cb321 4y agocython --annotate is an ok way to learn the whys & whereabouts of the rather hairy CPython API. That gives you an HTML page you can click on to expand your python code into equivalent-ish C API calls. Darker yellow means more calls, too. So, it's not a terrible start to do static analysis to guide optimization, but a combination score (with a run-time profile) would be even better. I believe there was a time very early on (like 2003) when there was discussion about maybe including Pyrex in CPython proper to get a more Common-Lisp like gradually typed system. (I mostly recall some comment of Greg's along the lines of being intimidated by such. I'm not sure how seriously the idea was entertained by PyCore.)
- bminusl 4y agoMaybe you will also be interested in Cython+: "Multi-core concurrent programming in Python" [0]. [0]: https://www.cython.plus/en/ https://www.cython.plus/en/
- gotaquestion 4y agoI like cython, but I think it pigeon-holes developers: i've seen hardware modules written in Cython, when they could easily switch to C++ and provide a library that could be used as an FFI in any language, but instead locked themselves (and users) into the narrow world of Python. Is there a way to convert a Cython module(s) to C++, or at least a .o file? They are so dang close.
- ok123456 4y agohttps://github.com/Nuitka/Nuitka https://github.com/Nuitka/Nuitka
- blindseer 4y agoI wish Cython was a more popular option than choosing Julia or Go. Cython is great and you can get some real performance out of it. The only drawback is that a Cython module still loads the CPython interpreter, so I personally prefer writing performance critical code in Rust instead. Writing in Julia has the same drawbacks of not being embeddable that writing in Cython does. Julia has multiple dispatch and may seem more appealing but at scale it is a very slow language to develop in. And for scripts it takes FOREVER (try loading Plots, CSV, DataFrames, Makie etc every time you restart. It’s genuinely insane that that’s the norm.) If the whole Python ecosystem was in Cython (i.e. numpy, scipy, etc) I’d never use another backend language again.
- nickmain 4y agoMypyc is an alternative tool for speeding up type-annotated Python code. It doesn't help with calling existing C code, unfortunately. [0] https://mypyc.readthedocs.io/en/latest/ https://mypyc.readthedocs.io/en/latest/
- victoryhb 4y agoI tried to learn Cython last year, but was thwarted by two issues: (1) its syntax was too ugly for my taste and support for the pure Python mode was immature; (2) performance bottlenecks were opaque and hard to profile (at least for beginners). I ended up picking up Nim, a language with Python-like syntax and C-like performance, and was productive within hours (literally). I never looked back.