w3resource

What is cProfile in Python?

Understanding cProfile in Python

'cProfile' is a built-in Python module used for profiling Python code. It provides deterministic profiling, which means that profiling results are repeatable and consistent across multiple runs of the same program. With 'cProfile', developers can identify performance bottlenecks, understand the execution time of different functions, and optimize their code.

'cProfile' uses statistical sampling to record the time spent on each function and the number of times each function is called. Following that, it generates a detailed report showing the execution time and call count for each function. This allows developers to analyze their code's performance comprehensively.

Here's how to use cProfile to profile Python code:

Using cProfile via Command Line:

We can run a Python script with cProfile directly from the command line using the -m cProfile option:

python -m cProfile my_script.py

This will execute test.py with cProfile enabled, and the profiling results will be displayed on the console.

Using cProfile Programmatically:

If we want to profile specific parts of your code within a Python script, you can use the cProfile.run() function:

import cProfile
def test_function():
    # Function to be profiled
    pass
cProfile.run(test_function()')

When test_function() is called, cProfile will profile the function and produce the profiling results.

Sorting and Visualizing Results:

By default, 'cProfile' provides raw statistical data. However, it can generate a more readable and sorted output by using the '-s' option with sorting criteria. For example,

python -m cProfile -s time my_script.py

With this command, we can sort the profiling results based on the time spent in each function, making it easier to identify the most time-consuming ones.

Using pstats Module for Analysis:

After profiling your code, we can save the profiling data to a file and analyze it later using the 'pstats' module. For example,

import cProfile
import pstats
def test():
    # Function to be profiled
    pass
cProfile.run(test()', 'profile_results')
stats = pstats.Stats('profile_results')
stats.sort_stats('time').print_stats()

This will save the profiling data to the file profile_results and then print the sorted statistics based on time spent in each function.



Follow us on Facebook and Twitter for latest update.