Code

The measuring harness

The heap works and the tests pass. Now two files that let you measure it: the harness and a main.go with two new commands.

Both are given complete: they are an instrument, not an exercise. You do not write them.

command what it does
algo top the best k: heap against "sort and take"
algo pq lesson 6's question: your heap against container/heap

heapbench.go also carries stdItems — the interface container/heap demands. Look at its signatures now; in step 7 they are the entire answer:

func (s *stdItems) Push(x any)  { *s = append(*s, x.(Item)) }
func (s *stdItems) Pop() any    { ... }

What they do

  • algo top — the partial-order argument: you do not have to sort to find the best ten. Your TopKHeap against "sort everything and take the first k" (topKSort). You read its output in step 6.
  • algo pq — lesson 6's question: your heap against container/heap, on the same job — push everything and drain it, like Dijkstra's frontier. Step 7.

The test file also carries four Benchmarks — step 7 needs them, because what it measures is something the counter cannot see:

go test -run XXX -bench 'Heap|TopK' -benchtime=20x -count=5