Loading video player…

Profiling Performance in Python (Overview)

Do you want to optimize the performance of your Python program to make it run faster or consume less memory? Before diving into any performance tuning, you should strongly consider using a technique called software profiling. It may help you answer whether optimizing the code is necessary and, if so, which parts of the code you should focus on.

Sometimes, the return on investment in performance optimizations just isn’t worth the effort. If you only run your code once or twice, or if it takes longer to improve the code than execute it, then what’s the point?

When it comes to improving the quality of your code, you’ll probably optimize for performance as a final step, if you do it at all. Often, your code will become speedier and more memory efficient thanks to other changes that you make. When in doubt, go through this short checklist to figure out whether to work on performance:

  1. Testing: Have you tested your code to prove that it works as expected and without errors?
  2. Refactoring: Does your code need some cleanup to become more maintainable and Pythonic?
  3. Profiling: Have you identified the most inefficient parts of your code?
Download

Cheat Sheet (.zip)

39.4 KB
Download

Course Slides (.pdf)

4.0 MB
Download

Sample Code (.zip)

2.7 KB

00:00 Welcome to the Profiling in Python video course. I’m Negar from Real Python, and I’ll be your guide on your journey into the world of profilers. What is profiling?

00:10 When do you need it, and how can it actually help you improve your code? Let’s start with a real-life scenario. You’re working on an e-commerce site with a shopping cart that has three main functions: add_item() that adds the product to the user’s cart, then you have calculate_total(), which adds up all item prices, including tax and shipping, and you have save_cart(), which stores the cart in the database.

00:37 And your program is working. But there’s a problem. Your customers are trying to buy multiple items, but each time they click add to cart, they have to wait around eight whole seconds.

00:51 You have to fix the slowdown, but where do you even start? Maybe the issue is in add_item(). It could be making unnecessary API calls, or maybe it’s calculate_total() doing something inefficient like recalculating shipping and tax in a loop instead of caching results.

01:09 It might even be save_cart() taking too long to write to the database because of poor indexing. But then you’ll just be guessing, and that means you would spend hours trying to fix the wrong part of the code.

01:21 How can you know for sure which function is to blame?

01:25 Your savior is profiling. It shows you exactly what’s making your program run slowly. So how do you actually profile your code and measure how much time each function is taking?

01:38 That’s exactly what this course is all about. In the next lessons, you’ll understand when and why to profile your code. You’ll use one of Python’s built-in timing modules called timeit to measure the average runtime of a function.

01:53 You’ll also learn the difference between different profilers like deterministic and statistical. And finally, you’ll investigate two main profilers called c Profile and Pyinstrument to identify performance bottlenecks in your program.

02:09 Time to get started. Next up is all about when you need to profile your code. Profile before you optimize.

Become a Member to join the conversation.