Book Review: Mastering Python High Performance

Packt Publishing recently sent me a copy of Mastering Python High Performance by Fernando Doglio. They also had me be a technical reviewer of the book before its publication. Anyway let’s do a quick review and if you think it sounds interesting, you can check out my full review too!


Quick Review

  • Why I picked it up: I got it for free, but I did find the title intriguing.
  • Why I finished it: As a technical reviewer of the book, I had to read it all the way through. However, it has a lot of interesting concepts and it was short.
  • I’d give it to: Someone who needs to learn about how to increase their Python code’s efficiency.


Book Formats

You can get an eBook (PDF, EPUB or MOBI) version or a softcover.


Book Contents

The book is split up into 8 chapters with 235 pages.


Full Review

When I was originally reading this book, I was interested in seeing how the author would speed up their code. He covers a wide variety of topics which is good, but that also hampers the book as none of the topics are covered in depth. He uses lots of small examples and shows how to profile before moving on to optimizing them. Let’s go over each of the chapters so you can get a taste of what the book covers.

Chapter one is all about profiling your code. It covers the differences between statistical and event-based profiling, what profiling is and why it’s important, bottlenecks and memory leaks. It also goes over running time complexity (linear, factorial, quadratic, etc) and profiling best practices.

Then we logically move into chapter two where we learn about some profilers we can use with Python. The two that are covered are cProfile (included with Python) and line_profiler. The author demonstrates various ways to use cProfile to measure your code and also how to use Python’s pstats module, which is used for analyzing the results you receive from cProfile. Then the author moves on to using line_profiler and kernprof to analyze the same (or similar) examples used with cProfile. I think this is one of the best balanced chapters in the book and really quite interesting all by itself.

Chapter three goes into using visual tools to help you understand your profiler’s output. In this chapter, you will learn about KCacheGrind / pyprof2calltree and RunSnakeRun. For the most part, you’ll just learn how to use these tools to figure out what your data means.

In chapter four, we learn about optimizing your code. Topics covered are memoization, list comprehensions, generators, ctypes, loading your own custom C library, and some others tips and tricks. While this chapter is short, it has some good ideas and is worth reading or at least skimming.

Chapter five digs into multithreading and multiprocessing. You’ll learn about the pros and cons of each. You will also learn about the Global Interpreter Lock and how that affects you when you choose one of these techniques.

Chapter six goes into using PyPy and Cython and how they can be useful for additional optimizations to your code. I enjoyed this chapter, although I didn’t feel that PyPy got as much attention as Cython. There also weren’t very many coding examples.

If you’re into number crunching, then chapter seven is for you. It goes over how to use Numba, Parakeet and pandas. Quite frankly, out of the three of libraries, I had only ever heard of pandas. I personally don’t need to do a lot of number crunching in my line of work, but it was interesting to see how each of the libraries worked and get a general idea of what they could be used for.

Finally in chapter eight, the author attempts to put it all together. This chapter probably should have been twice as long as it is so that he could have actually covered everything. But in the end, it covers just barely enough and you do get to see a full example get optimized from beginning to end.

Overall, I enjoyed this book. I would recommend this to anyone who needs ideas for optimizing their Python code or just for learning about profiling in general.

Mastering Python High Performance

by Fernando Doglio

Amazon


Other Book Reviews