Member-only story
How to assess your code performance in Python
cProfile and line_profiler
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
andprofile
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
.