2 min read

Python Profiling (cProfile)

cProfile is a built-in Python module for profiling the performance of your Python programs. It provides deterministic profiling of Python programs, meaning it measures the time spent in each function.

Basic Usage

Command Line

You can run cProfile directly from the command line to profile a script.

python -m cProfile myscript.py

To sort the output by cumulative time (time spent in the function and all sub-functions), use the -s option:

python -m cProfile -s cumtime myscript.py

Inside Python Code

You can also use cProfile within your Python code.

import cProfile

def my_function():
    total = 0
    for i in range(10000):
        total += i
    return total

cProfile.run('my_function()')

Analyzing Output with pstats

For more complex analysis, you can save the profile data to a file and analyze it using the pstats module.

import cProfile
import pstats

def my_function():
    # ... some code ...
    pass

# Profile and save to file
cProfile.run('my_function()', 'output.prof')

# Analyze the saved data
p = pstats.Stats('output.prof')
p.sort_stats('cumulative').print_stats(10) # Print top 10 functions by cumulative time

Common Sort Keys

  • 'calls': Call count
  • 'cumulative': Cumulative time
  • 'file': File name
  • 'line': Line number
  • 'module': Module name
  • 'name': Function name
  • 'nfl': Name/file/line
  • 'pcalls': Primitive call count
  • 'stdname': Standard name
  • 'time': Internal time (time spent in the function itself, excluding sub-functions)

programming/python/python