Throughout Volume 1, we’d worked with the idea that an algorithm’s speed is measured by counting its steps. However, you saw in the previous chapter that sometimes simply counting steps isn’t enough to determine which algorithms are faster than others. For example, both Quicksort and Mergesort take roughly the same number of steps, yet computer scientists report that Quicksort is significantly faster in actual time. Again, by actual time I mean time measured in minutes and seconds rather than in steps.
How do the computer scientists know this? More importantly, how can you know which algorithms are truly the fastest?
Here enters a technique known as benchmarking. In this chapter, you’ll learn what benchmarking is and how to benchmark Python code. Along the way, you’ll also learn about nefarious benchmarking traps and how to avoid them.