──────────────────────────────────────

58. Radio Shift Diagnostics · Measure a Function Call

Measure elapsed seconds around one call and test the calculation with known readings.

Lesson files · lessons/debug-and-measure/time-work-with-a-clock

Before you start
- Pass a function as an argument without calling it first.
- Use a function default and unpack two returned values.

By the end
- Measure a call with two readings of time.perf_counter().
- Use a fake clock to check a known duration and a zero duration.
- Return the result of the work together with the elapsed seconds.

Where you can use it
A volunteer can time how long the station’s planning script takes and check the calculation without relying on the speed of a device.

Read time.perf_counter() before work starts and again after it finishes. Subtract the first reading from the second to get elapsed seconds. Multiply by 1,000 to display milliseconds. Measure the same work several times before judging speed, since a busy computer or a pause can affect the duration.

Keep perf_counter as the default and accept a clock function as an argument. The example supplies a fake clock that returns 8.0 and then 8.125. These readings give the same duration in every test. Returning both the result and elapsed seconds lets the caller use the result as usual.

Predict
What elapsed time prints for the fake readings 8.0 and 8.125?
- 8125 ms
- 125 ms

Worked example (example.py)

from time import perf_counter


def measure(work, clock=perf_counter):
    started = clock()
    result = work()
    return result, clock() - started


def make_labels():
    return [f"Clip {number}" for number in range(1, 4)]


readings = iter([8.0, 8.125])


def fake_clock():
    return next(readings)


labels, elapsed = measure(make_labels, clock=fake_clock)
print(f"Labels: {len(labels)}")
print(f"Elapsed: {elapsed * 1000:.0f} ms")
_, real_elapsed = measure(make_labels)
print(f"Real elapsed time is nonnegative: {real_elapsed >= 0}")


Example result
Labels: 3
Elapsed: 125 ms
Real elapsed time is nonnegative: True

Complete measure(work, clock=perf_counter). Read clock() before and after calling work() once. Return the result and the elapsed seconds as a pair. The first input line holds whole-number clip durations. The second holds the two fake clock readings in seconds. Keep the supplied output formatting and real clock default.

Starter program (starter.py)

from time import perf_counter


def measure(work, clock=perf_counter):
    return work(), 0


durations = [int(value) for value in input().split()]
readings = iter(float(value) for value in input().split())


def fake_clock():
    return next(readings)


def prepare_total():
    return sum(durations)


total, seconds = measure(prepare_total, clock=fake_clock)
print(f"Minutes: {total}")
print(f"Elapsed: {seconds * 1000:.0f} ms")


Starter input
5 3
12.0 12.5

Hint
Save started = clock() before calling work(). Subtract started from the second reading.

Case 1. Try the sample
Input
5 3
12.0 12.5
Expected output
Minutes: 8
Elapsed: 500 ms

Case 2. The clock readings are equal
Input
6
4 4
Expected output
Minutes: 6
Elapsed: 0 ms

Case 3. A duration over one second
Input
0 0
2.25 3.5
Expected output
Minutes: 0
Elapsed: 1250 ms

Stretch
In a local Python run, time the same workload three times using the default clock. Keep the input constant, write down the three durations, and describe any variation you see.

Reflect
What exactly does the fake clock test, and what can you learn only from the real clock?

Check your prediction
125 ms The timer subtracts the starting reading before it converts seconds to milliseconds.
