Challenge 1: Profile Two Approaches — Possible Solution ==================================================================== import cProfile def sum_squares_loop(): total = 0 for n in range(1_000_000): total += n ** 2 return total def sum_squares_builtin(): return sum(n ** 2 for n in range(1_000_000)) print("Manual loop:") cProfile.run("sum_squares_loop()") print("\nBuilt-in sum():") cProfile.run("sum_squares_builtin()") Output (exact timings will vary, but the shape is consistent): Manual loop: 3 function calls in 0.187 seconds ncalls tottime percall cumtime percall filename:lineno(function) 1 0.187 0.187 0.187 0.187 script.py:3(sum_squares_loop) Built-in sum(): 1000003 function calls in 0.098 seconds ncalls tottime percall cumtime percall filename:lineno(function) 1 0.061 0.061 0.098 0.098 script.py:8(sum_squares_builtin) 1000000 0.037 0.000 0.037 0.000 script.py:8() WHY THIS WORKS AS AN ANSWER ------------------------------ sum_squares_loop() reuses the chapter's own manual-loop pattern exactly — a plain for loop with total += n ** 2, the loop-accumulation style from Course 1, Chapter 4, run interpreted step by step in Python bytecode for all 1,000,000 iterations. sum_squares_builtin() reuses the chapter's own sum(n ** 2 for n in range(...)) pattern exactly — a generator expression (Course 2, Chapter 5) feeding sum(), a built-in implemented in C. Even though the generator expression itself still runs once per number in Python, the actual SUMMING loop inside sum() runs in C, avoiding the interpreter overhead the manual version pays on every single += step. cProfile.run("sum_squares_loop()") and cProfile.run ("sum_squares_builtin()") reuse this chapter's own programmatic cProfile call exactly, run once per function so their tottime values can be compared directly against each other rather than mixed together in one combined profile. Comparing the two tottime columns is the direct, measured version of this chapter's own claim that built-ins beat manual loops — the built-in version's own function body (tottime) is smaller because the heavy lifting happens inside sum()'s C implementation, which cProfile still accounts for separately but which runs fundamentally faster than the interpreted Python loop in the manual version.