Intermediate LevelPython
How do you profile deterministic execution bottlenecks in Python scripts using cProfile and SnakeViz?
❓ Interview Prompt: Explain `cProfile` deterministic profiler, profiling stats (`tottime`, `cumtime`, `ncalls`), and visualizing Sunburst call graphs with SnakeViz.
'cProfile' is Python's built-in C-extension deterministic profiler for identifying execution bottlenecks:
Profiling Workflow:
1
Profile script output: 'python -m cProfile -o profile.prof script.py'
2
Inspect call stats: 'tottime' (time inside function itself) vs 'cumtime' (total time including sub-calls).
3
SnakeViz: Visualize 'profile.prof' in an interactive browser Sunburst call graph!
Code Implementation Example
bash
# Profile Python script and generate profile artifact
python3 -m cProfile -o output.prof my_script.py
# Visualize interactive call graph in web browser
snakeviz output.prof💡Profiling script performance with cProfile and visualizing with SnakeViz.
🎯 Interview Key Takeaway
Use cProfile and SnakeViz to pinpoint exact function latency bottlenecks before optimizing code.
#cProfile#SnakeViz#Profiling#Performance#Intermediate
Back to all Python Questions→