Performance profiling in Python can help identify slow sections of code and improve overall performance. The cProfile module is a built-in tool for profiling Python programs, providing valuable insight into the execution time of different functions or methods.
Here are 10 Python code snippets demonstrating how to use cProfile to profile code and identify performance bottlenecks:
1. Basic Profiling with cProfile
import cProfiledefslow_function(): total =0for i inrange(1000000): total += ireturn total# Profile the slow functioncProfile.run('slow_function()')
This is the simplest usage of cProfile. It runs the function slow_function and outputs a profile of how long each part of the code took to execute.
2. Profiling Multiple Functions
import cProfiledeffunction_a(): total =0for i inrange(500000): total += ireturn totaldeffunction_b(): total =0for i inrange(1000000): total += ireturn total# Profile both functionscProfile.run('function_a()')cProfile.run('function_b()')
This example profiles two functions separately to observe their individual performance.
3. Profiling Code in a Script
You can use cProfile.run() to profile the main execution of a script.
4. Saving Profile Data to a File
This code saves the profiling data to a file named profile_data.prof for further inspection.
5. Viewing Profiling Results with pstats
This snippet loads the profiling data from a file and sorts the output by cumulative time. The print_stats(10) method prints the top 10 functions by cumulative time.
6. Profiling with the time Method for Comparison
This code compares cProfile output with a simple manual timing approach using time.time().
7. Profiling Code with a Decorator
This example defines a custom decorator profile_function to profile the execution of any function it wraps.
8. Profiling Using a Context Manager
Here, the context manager profile_context is used to start and stop profiling for a specific block of code.
9. Profiling External Libraries
This example demonstrates profiling a function that includes an external library (in this case, time.sleep).
10. Profiling with runctx for Code with Context
This snippet demonstrates profiling code within a specific namespace using cProfile.runctx().
These examples show how to use cProfile in various scenarios, from simple function profiling to profiling code with decorators and context managers. You can utilize these techniques to identify performance bottlenecks in your Python code and optimize its execution.
import cProfile
def main():
total = 0
for i in range(1000000):
total += i
print(total)
# Profile the main function
if __name__ == '__main__':
cProfile.run('main()')
import cProfile
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
# Save the profiling data to a file
cProfile.run('slow_function()', 'profile_data.prof')
import cProfile
import pstats
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
# Save the profiling data to a file
cProfile.run('slow_function()', 'profile_data.prof')
# Load and view the profiling results
stats = pstats.Stats('profile_data.prof')
stats.sort_stats('cumulative').print_stats(10)
import cProfile
import time
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
# Using cProfile to profile the function
cProfile.run('slow_function()')
# Using time to manually measure execution time
start = time.time()
slow_function()
end = time.time()
print(f"Manual time: {end - start} seconds")
import cProfile
def profile_function(func):
def wrapper(*args, **kwargs):
profiler = cProfile.Profile()
profiler.enable()
result = func(*args, **kwargs)
profiler.disable()
profiler.print_stats()
return result
return wrapper
@profile_function
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
# Execute the function and profile it
slow_function()
import cProfile
from contextlib import contextmanager
@contextmanager
def profile_context():
profiler = cProfile.Profile()
profiler.enable()
yield profiler
profiler.disable()
profiler.print_stats()
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
# Using the context manager to profile the function
with profile_context() as profiler:
slow_function()
import cProfile
import time
# Simulate some library functions
def simulate_external_lib():
time.sleep(2)
return sum([i for i in range(1000)])
def main():
simulate_external_lib()
# Profile the execution of the main function
cProfile.run('main()')
import cProfile
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
# Using runctx to profile code within a specific context
namespace = {}
cProfile.runctx('slow_function()', globals(), namespace)