The measuring stick
Now the measuring stick. This is the only code in the course you are handed finished. Everything else you write yourself.
You get it for two reasons: counting is not the interesting part of any algorithm, and every lesson has to measure with the same instrument or the numbers cannot be compared.
Create metrics/counter.go and main.go (see the panel).
Why we count operations instead of seconds
Run your search over a thousand items and time it with the clock. Here is what you actually get (Windows, real run):
n = 1000, searching for a title that is not there
run 1: wall 0s comparisons 1000
run 2: wall 0s comparisons 1000
run 3: wall 0s comparisons 1000
run 4: wall 0s comparisons 1000
run 5: wall 0s comparisons 1000
The clock says 0 s. Not "fast" — zero. The work happened: a thousand comparisons, five times running, the same number each time. The operating system's clock is simply too coarse to see it.
This is not a made-up example. It is a bug that was found and fixed in the
transit app this lesson's figures come from: sub-millisecond runs timed as
"0s", "which would have made the fastest implementation look untimed rather
than fast".
A comparison count has none of those problems:
- it is exact — 1000, not "about a thousand";
- it is deterministic — run it a hundred times, get the same answer;
- it is machine-independent — your number matches your classmate's, even if their laptop is twice as fast.
That last property is what makes every later lesson's test possible: a test can demand "no more than this many comparisons" and mean the same thing for everyone.