Python
Profiling
Code Optimization
Performance Analysis
Python Script

How do I profile a Python script?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Profiling a Python script is an essential practice when optimizing code. It helps in identifying bottlenecks, measuring code performance, and understanding memory usage. Profiling can be accomplished through different techniques and tools, and involves measuring the space (memory) and time complexity of a program.

Understanding Profiling

Profiling is the process of measuring a program's resource usage through its execution. The primary focus here is on CPU time and memory consumption, though other metrics like I/O operations can be considered. By obtaining these metrics, a developer can decide which parts of the code need optimization.

Profiling Techniques

1. cProfile

cProfile is a built-in Python module used for profiling. It provides a set of statistics detailing how long each function takes to execute and how often each function is called.

Example:

python
1import cProfile
2
3def sample_function():
4    total = 0
5    for i in range(10000):
6        total += i
7    return total
8
9cProfile.run('sample_function()')

Output:

plaintext
1         4 function calls in 0.007 seconds
2
3   Ordered by: standard name
4
5   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
6        1    0.003    0.003    0.007    0.007 <ipython-input-1-9c0d2c>:3(sample_function)
7        1    0.000    0.000    0.007    0.007 <string>:1(<module>)
8        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
9        1    0.004    0.004    0.004    0.004 {method 'disable' of '_lsprof.Profiler' objects}

2. line_profiler

For more granular performance data, line_profiler is extremely helpful. It profiles the time consumption line-by-line within functions.

Installation:

bash
pip install line_profiler

Usage Example:

Firstly, add decorators to the functions you want to profile:

python
1@profile
2def sample_function():
3    total = 0
4    for i in range(10000):
5        total += i
6    return total

Run the Python script with:

bash
kernprof -l -v script.py

3. memory_profiler

memory_profiler helps track the memory usage of a Python program line-by-line.

Installation:

bash
pip install memory_profiler

Usage Example:

Add decorators to profile memory usage:

python
1from memory_profiler import profile
2
3@profile
4def sample_function():
5    total = [i for i in range(10000)]
6    return total
7
8sample_function()

Execute the script, which will provide detailed memory usage:

bash
python script.py

4. Py-Spy

Py-Spy is a sampling profiler which is non-intrusive, and doesn't require code modifications. It can be used to profile running processes.

Installation:

bash
pip install py-spy

Usage Example:

To profile a running Python process (use the PID of the process):

bash
py-spy top --pid <pid>

Or, to create a flame graph, execute:

bash
py-spy record -o profile.svg --pid <pid>

Profiling Summary

The following table summarizes the key points for different profiling tools:

ToolProsConsSuitable for
cProfileBuilt-in, easy to use, comprehensiveOverheads, lacks granularityGeneral CPU profiling
line_profilerDetailed line-by-line time consumptionNeeds decorators, manual setupIn-depth CPU analysis
memory_profilerMemory tracking, ease of useUses decorators, slower performanceMemory usage checks
Py-SpyNon-intrusive, real-time profilingRequires external installationSampling, long-running programs

Best Practices for Effective Profiling

  1. Identify the Problem First: Profiling should be problem-oriented. If you know the performance metric you're interested in, it’s easier to choose the correct profiling tool.
  2. Start with a Broad Scope and Zoom In: Use cProfile for a high-level insight and then drill down using more specific tools like line_profiler or memory_profiler.
  3. Profile in Similar Conditions: Ensure that the script runs in an environment as similar as possible to the production environment, as external factors can skew the results.
  4. Iterative Optimization: Profile, optimize, and then profile again. Validate that each change leads to a real improvement.
  5. Use Aggregated Data: In cases of sampling, use average values over multiple runs to ensure the data is reliable.

Profiling reveals where a script uses the most resources, offering clues on where to focus optimization efforts. It is a critical skill in the toolbox of performance-aware developers.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.