How to use Profilers in Python

0 min read 167 words

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:

python app.py

We can run it with a Profiler by doing the following:

python -m cProfile -o outfile app.py

This will give you a report that shows where most of the time is spent while running your app.

My preferred option, is to use the line_profiler tool to perform a more detailed scan.

Start by installing the line_profiler using pip.

pip install line_profiler

Then we can adjust our application code and add the @profile annotation right before each function you’d like to see statistics with.

@profile
def fun_a():
    #do something

@profile
def fun_b():
    #do something more

if __name__ == '__main__':
    fun_a()
    fun_b()

Now we can run the profiler using the below command:

time kernprof -l -v app.py
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags