Build software that combines Python’s expressivity with the performance and control of C (and C++). It’s possible with Cython, the compiler and hybrid programming language used by foundational packages such as NumPy, and prominent in projects including Pandas, h5py, and scikits-learn. In this practical guide, you’ll learn how to use Cython to improve Python’s performance—up to 3000x— and to wrap C and C++ libraries in Python with ease.
Author Kurt Smith takes you through Cython’s capabilities, with sample code and in-depth practice exercises. If you’re just starting with Cython, or want to go deeper, you’ll learn how this language is an essential part of any performance-oriented Python programmer’s arsenal.
Use Cython’s static typing to speed up Python codeGain hands-on experience using Cython features to boost your numeric-heavy PythonCreate new types with Cython—and see how fast object-oriented programming in Python can beEffectively organize Cython code into separate modules and packages without sacrificing performanceUse Cython to give Pythonic interfaces to C and C++ librariesOptimize code with Cython’s runtime and compile-time profiling toolsUse Cython’s prange function to parallelize loops transparently with OpenMP
My browser just ate the entire original version of this review because I'm a klutz. But here's the short version.
- Python is slow. C is fast. - Programming in Python is easy. Programming in C is hard. - Cython bridges the gap. Write Python, convert it to C incrementally. - This book does a great job of walking you through the process and teaching you how to get the most out of one of the best things to happen to Python since Guido van Rossum himself. - It's aimed mainly at number-crunchers -- lots of focus on integration with Numpy -- but the motivated programmer can easily see how to apply it to many other fields. (In my case, I wrote a video game and got massive speedups by moving performance-crucial code to C with Cython, mainly the collision detection algorithms.) - Not a long book, but that adds to its accessibility. - Anyone remotely interested in enhancing Python performance where it's warranted* deserves to pick this up.
* Python's big appeal is that it swaps programmer convenience for raw performance. A script thrown together in ten minutes that takes two minutes to run will always beat a program that takes two days to put together but executes in a tenth of a second. But with Cython, we're reaching a point where we don't have to compromise.
Kurt Smith's Cython is very clear. With only a few days of reading and coding, I was able to speed up my pure-Python simulation from 6 s per iteration to just 30 ms! If you're simulating physics in Python on a workstation, I highly recommend this book.
Really nice if you want to get familiar with lower-level implementation with Python, especially for scientific computing. I enjoyed the sections on typed memoryviews and OpenMP (prange) + concrete examples with speedups measure.