Now that we know about coding functions and iteration tools, we’re going to take a short side trip to put both of them to work. This chapter closes out the function part of this book with a larger case study that times the relative performance of the iteration tools we’ve met so far.
Along the way, this case study surveys Python’s code timing tools, discusses benchmarking techniques in general, and allows us to explore code that’s a bit more realistic and useful than most of what we’ve seen up to this point. We’ll also measure the speed of current Python implementations—a data point that may or may not be significant, depending on the type of code you write.
Finally, because this is the last chapter in this part of the book, we’ll close with the usual sets of “gotchas” and exercises to help you start coding the ideas you’ve read about. First, though, let’s have some fun with a tangible Python application.
We’ve met quite a few iteration alternatives in this book. Like much in programming, they represent tradeoffs—in terms of both subjective factors like expressiveness, and more objective criteria such as performance. Part of your job as a programmer and engineer is selecting tools based on factors like these.
In terms of performance, I’ve mentioned a few times that list
comprehensions sometimes have a speed advantage over
for loop statements, and that
map calls can be faster or slower than both depending on call patterns. ...