Skip to main content

Week 11 Bench: Rewriting for constant time

Taking a function that leaks through its running time and rewriting it so the leak is gone, not just smaller

3 May 2027

Before the Bench

Bring the timing harness from week 4’s session — this week reuses it to grade your own rewrite rather than a supplied example.

In the Bench

Take a function you are given — deliberately written with an early-exit comparison, much like week 4’s — and rewrite it so every branch it takes is independent of the secret. Then measure it exactly as week 4 measured the original, and confirm the trend against agreement length is gone. Defence is not the same skill as detection: it is entirely possible to correctly diagnose a leak and still write a “fix” that only makes it harder to see.

Afterwards

Keep both versions and both timing plots. They are the clearest single before-and-after pair the course produces, and the closing lecture draws on exactly this contrast.

The spec

What you bring to the session. Written so a reader can tell whether it has been met without asking you.

Teaching team