Member-only story

How to assess your code performance in Python

cProfile and line_profiler

Sophia Yang, Ph.D.
Towards Data Science
3 min readDec 23, 2020

--

Profiling helps us identify bottlenecks and optimize performance in our code. If our code runs slowly, we can identify where our code is slow and then make corresponding improvements.

Here are the explanations of Python profilers from the Python documentation:

cProfile and profile provide deterministic profiling of Python programs. A profile is a set of statistics that describes how often and for how long various parts of the program executed…cProfile is recommended for most users; it’s a C extension with reasonable overhead that makes it suitable for profiling long-running programs.

cProfile is a Python built-in profiler, which is great for measuring the time spent at each function and how many times each function is executed. Let’s start with an example of cProfile.

cProfile

Here is a Python script solving a linear system with the conjugate gradient method. Details of the calculation can be found in my other article. To run this Python script, we can simply do python conjugate_method_cprofile.py.

--

--

Towards Data Science
Towards Data Science

Published in Towards Data Science

Your home for data science and AI. The world’s leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.

No responses yet