When you have performance problems in your Python application, you can use a Profiler to help you.
Step 1: Using cProfile
Your first option is to run your application with
-m cProfile in the cli.
Let’s take an example application that is run as follows:
We can run it with a Profiler by doing the following:
This will give you a report that shows where most of the time is spent while running your app.
Step 2: Using LineProfiler (recommended)
My preferred option, is to use the
line_profiler tool to perform a more detailed scan.
Start by installing the
Then we can adjust our application code and add the
@profile annotation right before each function you’d like to see statistics with.
Now we can run the profiler using the below command: