Code Clinic - The Build - Book 9: Making Code Fast (Optimization)
Python Working code is the goal. Fast code is the finish. The last book in the track gives you eight professional techniques and, more usefully, the habit of measuring before you change anything. It defines what optimization is and is not, then shows why building a long string with join beats adding pieces together inside a loop. timeit puts real numbers on both versions so you stop guessing which one is quicker. map, filter, any and all replace whole loops with single expressions, and comprehensions do the same for building a list, in one line that reads better than the four it replaces. The chapter on dictionary lookups introduces Big-O in plain terms and shows the gap between scanning ten thousand records and fetching one by key, which is the biggest speed win most programs are missing. Generators come next, producing values only as they are wanted rather than building everything in memory first, along with the trap of trying to reuse one twice. The final chapter hands the arithmetic to NumPy and lets vectorization do in one call what a loop does slowly.
Twenty A4 pages, in full color. Written in short sentences and plain English, for readers whose first language is not English and clearer for everyone else because of it.
Every code example in this book was run before the page was printed, so the output you read is the output you get. Instant PDF download. By M.R. Sterling.