Timing Context Manager

easy
Unit 5 · Lists & Context Managers0/3 to milestone

with blocks guarantee clean-up even when errors happen. Write one that measures how long a block takes, which is handy for profiling slow LLM calls or data loads.

Implement a class Timer(clock=time.perf_counter):

  • Using it as with Timer() as t: records the start time on entry and the end on exit.
  • t.elapsed holds the duration in seconds after the block. It's None before the block finishes.
  • Timing still happens when the block raises, and the exception must propagate (don't swallow it).
  • t.laps is a list. Calling t.lap() inside the block appends the time since the start (or since the previous lap).

clock is injectable so tests can use a fake clock.

#context-managers#classes2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Timing Context Manager · TechGradify