Chapter 4. Designing for high performance

 

This chapter covers

  • Understanding time and space complexity
  • Measuring the complexity of your code
  • Choosing data types for different activities in Python

Once you’ve written working code, there’s usually additional work to do. You need your code to not only accomplish its task, but also to accomplish it quickly. The performance of your code is how well it utilizes resources like memory and time. Software that performs at an acceptable level, meaning that it utilizes resources efficiently and responds to tasks within a desirable time frame, is said to be performant.

Software performance affects real-world people every day, whether they’re trying to upload their latest selfies to Instagram or doing real-time market analysis to pick stocks. How performant software should be often comes down to user perception. If something feels instantaneous, it might be fast enough.

Software performance can also affect the bottom line. If your software requires you to store something on a disk or in a database, minimizing the amount of storage required will save you money. Software that informs money-making decisions can net you more money if it runs faster. Performance has real-world impact.

Human perception

Humans generally perceive changes faster than 100 ms as instantaneous. If they click a button and the screen responds in 50 ms, they’re happy. As responsiveness slows beyond 100 ms, people begin to notice the lag.

4.1. Hurtling through time and space

4.1.1. Complexity is a little . . . complex

4.1.2. Time complexity

4.1.3. Space complexity

4.2. Performance and data types

4.2.1. Data types for constant time

4.2.2. Data types for linear time

4.2.3. Space complexity of operations on data types

4.3. Make it work, make it right, make it fast

4.3.1. Making it work

4.3.2. Making it right

4.3.3. Making it fast

4.4. Tools

4.4.1. timeit

4.4.2. CPU profiling

4.5. Try it out

Summary

sitemap