86. Performance Profiling with cProfile
1. Basic Profiling with cProfile
import cProfile
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
# Profile the slow function
cProfile.run('slow_function()')2. Profiling Multiple Functions
import cProfile
def function_a():
total = 0
for i in range(500000):
total += i
return total
def function_b():
total = 0
for i in range(1000000):
total += i
return total
# Profile both functions
cProfile.run('function_a()')
cProfile.run('function_b()')3. Profiling Code in a Script
4. Saving Profile Data to a File
5. Viewing Profiling Results with pstats
6. Profiling with the time Method for Comparison
time Method for Comparison7. Profiling Code with a Decorator
8. Profiling Using a Context Manager
9. Profiling External Libraries
10. Profiling with runctx for Code with Context
runctx for Code with ContextLast updated