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. Not only do you need it to 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.

Human perception

Humans generally perceive changes faster than 100ms as instantaneous. If they click a button and the screen responds in 50ms, they’re happy. As responsiveness grows beyond 100ms, people begin to notice the lag. For long-running activities like downloading large files, lag can’t always be helped. In these cases, accurate progress updates are important because they change the perception of progress to feel faster.

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

4.6  Summary