Profiling Code Execution with cProfile
`cProfile` is Python's built-in deterministic C-extension profiler. It tracks call counts, per-function time, and cumulative execution duration to find bottlenecks 📊.
import cProfile
def slow_task():
return sum(i * i for i in range(10000))
def main():
for _ in range(5):
slow_task()
cProfile.run("main()")
# Outputs: ncalls, tottime, percall, cumtime 📊
# 82. Profiling Code Execution with cProfile `cProfile` is Python's built-in deterministic C-extension profiler. It tracks call counts, per-function time, and cumulative execution duration to find bottlenecks 📊. ```python import cProfile def slow_task(): return sum(i * i for i in range(10000)) def main(): for _ in range(5): slow_task() cProfile.run("main()") # Outputs: ncalls, tottime, percall, cumtime 📊 ``` Always check `cumtime` (cumulative time spent in that function and all sub-functions) to find which high-level modules are consuming your CPU! 📊 --- ### Question What metric in `cProfile` output indicates total time spent in a function including calls to other subfunctions? 🧠- [ ] a. `cumtime` (cumulative time) 📊 - [ ] b. `tottime` - [ ] c. `ncalls` - [ ] d. `percall` --- ### Challenge Import `cProfile`. Profile `sum(range(100))` using `cProfile.run(...)`. 🎯 <details> <summary>Solution</summary> ```python import cProfile cProfile.run("sum(range(100))") ``` </details>