What Is Profiling?
Profiling is the process of measuring the time and resources a Python program uses. It helps identify performance bottlenecks, such as slow functions or inefficient loops.
Built-in Python Profiling Tools
1. cProfile
– Recommended for Most Use Cases
python -m cProfile my_script.py
Output:
Shows how much time each function took, how many times it was called, and where it was called from.
Example: Profiling a Python Function
import cProfile
def slow_function():
total = 0
for i in range(10000):
for j in range(1000):
total += i * j
return total
cProfile.run('slow_function()')
Output (truncated):
10001 function calls in 1.234 seconds
ncalls tottime percall filename:lineno(function)
1 1.234 1.234 script.py:3(slow_function)
Understanding Key Stats
Column | Meaning |
---|---|
ncalls |
Number of times the function was called |
tottime |
Total time spent in the function (excluding subcalls) |
percall |
Average time per call |
cumtime |
Cumulative time, including sub-function calls |
Optional: Save and Sort Profiling Results
import cProfile
import pstats
cProfile.run('slow_function()', 'profile_output')
p = pstats.Stats('profile_output')
p.sort_stats('cumulative').print_stats(10) # Show top 10 slowest
Visual Profiling Tools
1. SnakeViz (visual graph)
pip install snakeviz
python -m cProfile -o profile.out my_script.py
snakeviz profile.out
2. line_profiler (line-by-line timing)
Install:
pip install line_profiler
Use with decorator:
@profile
def compute():
...
Run:
kernprof -l -v my_script.py
Use Case: Compare Algorithms
import time
def method1():
return sum([i for i in range(100000)])
def method2():
total = 0
for i in range(100000):
total += i
return total
start = time.time()
method1()
print("Method1:", time.time() - start)
start = time.time()
method2()
print("Method2:", time.time() - start)
Simple benchmarking, but use
cProfile
for deeper insights.
Best Practices
- Use profiling after code is functionally complete
-
Focus on functions with high
tottime
orcumtime
- Optimize critical paths, not everything
-
Prefer
cProfile
for general use andline_profiler
for precision
Conclusion
Profiling helps you go beyond guesswork and make data-driven optimizations in your Python code. Use tools like cProfile
, pstats
, and SnakeViz
to understand where your time goes and how to make your program faster.
0 Comments:
Post a Comment