Chapter 2. Analyzing Code Performance

In Chapter 1, I set out some of the aspects of writing good code, and in this chapter I’ll take a deep dive into one aspect: performance. If you’re writing code that forms part of a larger system, it is very important that your code is performant. It should return its outputs in a timely manner, and it shouldn’t exceed the computing resources available.

But first, the most important thing is that your code should work. Before applying any of the techniques in this chapter or the next, make sure that your code solves the problem that it needs to solve and returns the outputs that you expect. Any optimization or speedup should happen after the code is already running correctly.

Second, ask whether there is a requirement for improved performance. You’ll need to know the expectations of the larger system your code is interfacing with. Does your code need to return a result within a specific amount of time? Is your user waiting to take some action based on the results of your code? If so, the faster it works, the better the user’s experience.

For example, your machine learning model may need to return its prediction within 100 ms so that the prediction can be shown to the user. If your code already returns its result within the time allowed, there may be no need to spend time optimizing its performance.

It is, however, a really good idea to be aware of the principles in this chapter. It’s useful to know when you’re writing code that’s inefficient ...

Get Software Engineering for Data Scientists now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.