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.
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.