195. Python Profiling
1. Basic Profiling with cProfile
import cProfile
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
# Profiling the function
cProfile.run('slow_function()')2. Profiling a Function in a Larger Script
import cProfile
def function_a():
sum(range(10000))
def function_b():
for i in range(100):
sum(range(10000))
def main():
function_a()
function_b()
# Profiling the entire main function
cProfile.run('main()')3. Saving Profiling Results to a File
4. Using pstats to Analyze Profiling Data
5. Profiling with Different Sorting Options
6. Profiling Multiple Functions
7. Viewing Profiling Data in Graphical Format (using snakeviz)
8. Profiling a Class Method
9. Using profile for More Granular Control (Profiling a Specific Code Block)
profile for More Granular Control (Profiling a Specific Code Block)10. Profiling with a Decorator for Easy Reuse
Key Concepts:
Last updated